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.
Hey Mark,
Okay. Let's improve things quite a lot by getting rid of the UI-scripting and talking to Safari directly.
--
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
------------------------------------------------------------------------------------------------
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}
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
------------------------------------------------------------------------------------------------
Here is a somewhat streamlined version of your original script. The date handler seems a trifle complicated, but it's faster than the shell. (I know Nigel has a better one yet, but I didn't want to take the time to find it.)
------------------------------------------------------------------------------------------------
on getFomattedDate()
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 getFomattedDate
------------------------------------------------------------------------------------------------
tell application "Safari"
if front document exists then
activate
set theURL to (get URL of document 1)
tell application "System Events"
tell process "Safari"
click menu item "Select All" of menu "Edit" of menu bar 1
click menu item "Copy" of menu "Edit" of menu bar 1
end tell
end tell
else
return
end if
end tell
------------------------------------------------------------------------------------------------
delay 0.1
set textclip to (the clipboard)
------------------------------------------------------------------------------------------------
tell application "BBEdit"
activate
set newDoc to make new document
tell newDoc
set its text to (my getFomattedDate()) & return & return & theURL & return & return & textclip & return
tell its text
straighten quotes with replacing target
select insertion point before character 1
end tell
end tell
end tell
------------------------------------------------------------------------------------------------