• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Script performance and Safari
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Script performance and Safari


  • Subject: Re: Script performance and Safari
  • From: Christopher Stone <email@hidden>
  • Date: Sat, 14 Apr 2012 06:42:48 -0500

On Apr 13, 2012, at 10:07, Mark Ratledge wrote:
Is it possible to increase the performance of this script? It can take 3-4 seconds to run.
______________________________________________________________________

I've updated the script with a handler to resize and center the BBEdit window based on a percentile of the screen width.

On my i7 MacBook Pro the script takes ~ 0.6 seconds to process this page.

Just for fun I rewrote the script (appended) to use the Satimage.osax for find/replace actions and made it a bit faster.  The new script processes the page above in ~ 0.37 seconds.

Oh, these times were calculated when the scripts were run from FastScripts.

--
Best Regards,
Chris

------------------------------------------------------------------------------------------------
on dateStr()
tell (current date)
set d to (its month as number) & (its day as number) & (its year) mod (((its year) div 1000) * 1000)
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
set d to d as text
set AppleScript's text item delimiters to oldTIDS
return d
end tell
end dateStr
------------------------------------------------------------------------------------------------
on winBounds(percentScreenWidth)
tell application "Finder" to set {x1, y1, x2, y2} to bounds of window of desktop
set winWidth to (percentScreenWidth * x2) div 1
set newX1 to (x2 - winWidth) div 2
set newX2 to newX1 + winWidth
set newBounds to {newX1, 44, newX2, y2}
return newBounds
end winBounds
------------------------------------------------------------------------------------------------
tell application "Safari"
if front document exists then
tell front document
set theURL to its URL
set pageText to its text
end tell
end if
end tell
------------------------------------------------------------------------------------------------
set pageHdr to dateStr() & return & return & theURL & return & return
------------------------------------------------------------------------------------------------
tell application "BBEdit"
activate
set newDoc to make new document with properties {text:pageText}
set bounds of front window to winBounds(0.5) of me
tell newDoc
tell its text
straighten quotes with replacing target
replace "\\A\\s+" using "" options {search mode:grep, starting at top:true}
replace "\\s{2,}\\Z" using return options {search mode:grep, starting at top:true}
replace linefeed using return options {starting at top:true}
set before character 1 to pageHdr
select insertion point before character 1
end tell
tell its window
if soft wrap text = false then
set its soft wrap text to true
end if
end tell
end tell
end tell
------------------------------------------------------------------------------------------------




------------------------------------------------------------------------------------------------
# Uses the Satimage.osax for regex find/replace.
------------------------------------------------------------------------------------------------
on cng(findText, changeText, srcData)
change findText into changeText in srcData with regexp without case sensitive
end cng
------------------------------------------------------------------------------------------------
on dateStr()
tell (current date)
set d to (its month as number) & (its day as number) & (its year) mod (((its year) div 1000) * 1000)
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
set d to d as text
set AppleScript's text item delimiters to oldTIDS
return d
end tell
end dateStr
------------------------------------------------------------------------------------------------
on winBounds(percentScreenWidth)
tell application "Finder" to set {x1, y1, x2, y2} to bounds of window of desktop
set winWidth to (percentScreenWidth * x2) div 1
set newX1 to (x2 - winWidth) div 2
set newX2 to newX1 + winWidth
set newBounds to {newX1, 44, newX2, y2}
return newBounds
end winBounds
------------------------------------------------------------------------------------------------
tell application "Safari"
if front document exists then
tell front document
set theURL to its URL
set pageText to its text
end tell
end if
end tell
------------------------------------------------------------------------------------------------
set pageHdr to dateStr() & return & return & theURL & return & return
set pageText to (cng("\\A\\s+|\\s+\\Z", "", pageText)) & return
set pageText to cng(linefeed, return, pageText)
set pageText to pageHdr & pageText
------------------------------------------------------------------------------------------------
tell application "BBEdit"
activate
set newDoc to make new document with properties {text:pageText}
set bounds of front window to winBounds(0.5) of me
tell newDoc
tell its text
straighten quotes with replacing target
select insertion point before character 1
end tell
tell its window
if soft wrap text = false then
set its soft wrap text to true
end if
end tell
end tell
end tell
------------------------------------------------------------------------------------------------

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Script performance and Safari
      • From: KOENIG Yvan <email@hidden>
References: 
 >Script performance and Safari (From: Mark Ratledge <email@hidden>)
 >Re: Script performance and Safari (From: Christopher Stone <email@hidden>)

  • Prev by Date: Re: Bugs
  • Next by Date: Re: Bugs
  • Previous by thread: Re: Script performance and Safari
  • Next by thread: Re: Script performance and Safari
  • Index(es):
    • Date
    • Thread