Out of Memory for application, not script

Hello all, I’m hoping to tap into your collective wisdom again. :slight_smile:

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. :wink:


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

Not sure what’s causing the out of memory error, but I’ve got some help for the other issue:

The object you’re looking for is startup disk in the Finder’s dictionary:

tell application "Finder"
	set theDisk to startup disk
	return theDisk as text
end tell

Thanks! That’ll help with my portability. :slight_smile:

Talix,

my guess it that reading the text file in is causing the out of memory error. I had a similar problem once before. Its not really necessary to read in the file you can just write to the end of the file. I have modified your script so that it just write to the end of the file; this should fix the problem.

mm



(* This script adds the text of the status bar to a file for later processing *)

property filePath : ""

set filePath to (path to preferences folder as Unicode text) & "StoweMorris:DownloadStoryQueue.txt" as file specification
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)
-- write the link to the text file
WriteFile(theLink)

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

-- write the link to the end of the file
on WriteFile(theLink)
	try
		close access filePath --  just incase it was left open
	end try
	open for access filePath with write permission
	write (theLink & return) to filePath starting at eof
	close access filePath
end WriteFile

Awesome! That is so much better. :slight_smile: That’s the problem with my knowledge of Applescript; it always seems like I’m kludging a solution with the limited commands I know, rather than just using the correct commands. :stuck_out_tongue:

I don’t suppose I could get you to throw together a quick function that will read the same file this script is writing to, strip off one link and return it (from either end, but I prefer beginning), and write the rest back to the same file? :smiley:

Thanks muchly either way!

-Talix