• 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: Fri, 13 Apr 2012 15:46:10 -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.
______________________________________________________________________

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
------------------------------------------------------------------------------------------------

 _______________________________________________
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: Christopher Stone <email@hidden>
    • Re: Script performance and Safari
      • From: Alex Zavatone <email@hidden>
References: 
 >Script performance and Safari (From: Mark Ratledge <email@hidden>)

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