Hello all, I’m hoping to tap into your collective wisdom again.
I have the following script, which takes whatever link I’m hovering over in Safari and stores it in a text file. It runs fine when I run it out of Script Editor (adding a delay of a couple seconds so I can position my cursor), but it gives me an Out of Memory error when I run it as a compiled application. Anyone have an idea as to why that might be, and what I can do to fix it?
The only clue I can give you is that it was working fine until I just got a new computer (24" Intel iMac - glee! :D). Some of my scripts were broken until I remembered to turn “Enable Access for Assistive Devices” back on so my UI scripting would work, but I have done that now. I also had to change my computer name in my scripts that use it directly (is there a “path to hard drive” that I’m missing, like there is a “path to desktop”?), but that doesn’t apply here.
Thanks for your assistance!
-Talix
P.S. I always welcome recommendations for improving my utility functions (read/write file, etc.), if something jumps out at you.
(* This script adds the text of the status bar to a file for later processing *)
property filePath : ((path to preferences folder) as string) & "StoweMorris:DownloadStoryQueue.txt"
delay 2
-- get the text
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
set theLink to value of static text 1 of group 1 of window 1
end tell
end tell
-- extract the link
set theLink to CleanLink(theLink)
-- read the existing file
set fileText to ReadFile(filePath)
-- add new link and write back file
set fileText to fileText & return & theLink & return
WriteFile(fileText, filePath)
(*
------------------------------------------------------------------------
GENERIC UTILITY FUNCTIONS
------------------------------------------------------------------------
*)
-- removes non-link text
on CleanLink(theLink)
if length of theLink is less than 5 then
display dialog "Unknown link format: " & theLink buttons {"Cancel"} default button "Cancel"
return ""
end if
set testText to (characters 1 through 5) of theLink as text
if testText is equal to "Go to" then -- Safari (Go to "URL")
return characters 8 through ((length of theLink) - 1) of theLink as text
else if testText is equal to "Link:" then -- IE (Link: URL)
return characters 7 through (length of theLink) of theLink as text
else
display dialog "Unknown link format: " & theLink buttons {"Cancel"} default button "Cancel"
return ""
end if
end CleanLink
-- saves the fileText to the filePath as Unicode text, overwriting any existing file. If file is locked, delay two seconds and try again a few times.
on WriteFile(fileText, filePath)
set numTries to 0
set fileOpened to false
repeat while (numTries is less than 5 and fileOpened is false)
try
set fileRef to open for access filePath with write permission
set eof of fileRef to 0
write fileText to fileRef as Unicode text
close access fileRef
set fileOpened to true
on error
set numTries to numTries + 1
delay 2
end try
end repeat
if fileOpened is false then error filePath number 503 -- throw error if can't write file
end WriteFile
-- returns the entire contents of the file with the given path
on ReadFile(filePath)
tell application "Finder"
if (not (exists item filePath)) then return ""
end tell
set fileRef to open for access filePath
set fileText to ""
try
set fileContents to (read fileRef as Unicode text)
set fileText to (fileContents as string)
on error
close access fileRef
set fileRef to open for access filePath
set fileContents to (read fileRef)
set fileText to (fileContents as string)
end try
close access fileRef
return fileText
end ReadFile