This script works as a script, but not as an App

G’day

This has me flummoxed. The script works fine, until I save and use it as an App, then the ‘Write to file’ command writes a ‘long’ error to the file.
Any thoughts please.
EDIT I should have said that as an App, it only saves the position of 1 icon, then the rest is seen as ‘long’, even when I write them to the file individually


-- Restore Desktop Icons

-- Not Bloody Copyright
-- By Santa 2006
-- Sunny Land of Oz
-- bec9@tpg.com.au

-- Rough as Guts Software
-- Version 1.2

global ReturnIconsToPosition
set ReturnIconsToPosition to {}
global thelist
set thelist to {}
global WindowExists
set WindowExists to false
global thePath
global theStoragePath
global TheButton
set TheButton to {}

tell application "Finder"
	try
		set temp to name of the front window
		set WindowExists to true
	on error
		set WindowExists to false
	end try
end tell

if WindowExists then
	display dialog "There is a window '" & temp & "' open and at the front. " & return & return & "Do you want to Save or Restore the icon positions in...." & return & return & "the Window, " & return & "or, " & return & "the Desktop?" buttons {"Cancel", "The Window", "The Desktop"}
	set TheButton to the button returned of the result
	if TheButton = "The Desktop" then
		set WindowExists to false
		my DoDesktop()
	end if
	if TheButton = "The Window" then
		set WindowExists to true
		my DoDesktop()
	end if
else
	my DoDesktop()
end if
-- ===========================================================================
on DoDesktop()
	if my CheckIfFileExists() then
		set tempString to "Save the Desktop Icon Positions, or"
		if WindowExists then set tempString to "Save the Windows Icon Positions, or"
		
		display dialog "Do you want to..." & return & return & tempString & return & return & "Restore them back to a Saved Position?" buttons {"Cancel", "Save Positions", "Restore Positions"}
		set TheButton to the button returned of the result
		if TheButton = "Save Positions" then
			my GetPositions()
			if TheButton = "OK" then return
			my WritePositions()
		end if
		if TheButton = "Restore Positions" then
			my ReadOldPositions()
			my ReplacePositions()
		end if
	else
		if TheButton = "The Window" then
			display dialog "There does not appear to be any saved Icon positions for this Window." & return & return & "Do you want to save the Windows Icon Positions." & return buttons {"Cancel", "Save Window Positions"}
			set TheButton to the button returned of the result
			if TheButton = "Save Window Positions" then set WindowExists to true
			my GetPositions()
			if TheButton = "OK" then return --<-- Nothing to be saved
			my WritePositions()
		else
			display dialog "There does not appear to be any saved positions for the Desktop." & return & return & "Do you want to save the Desktop Icon Positions." & return buttons {"Cancel", "Save Desktop Positions"}
			set TheButton to the button returned of the result
			if TheButton = "Save Desktop Positions" then set WindowExists to false
			my GetPositions()
			if TheButton = "OK" then return --<-- Nothing to be saved
			my WritePositions()
		end if
	end if
end DoDesktop

on GetPositions()
	tell application "Finder"
		try
			if WindowExists then
				set temp to name of the front window
				select every item of window temp
			else
				select every item of the desktop
			end if
			set thelist to (selection as list)
			if number of items in thelist = 0 then
				display dialog "Please only use me if you have some icons for me to remember positions for." buttons {"OK"} --<-- Nothing to be saved
				set TheButton to the button returned of the result
				if TheButton = "OK" then return
			end if
			set x to 0
			repeat with distribute in thelist
				if WindowExists then
					set end of ReturnIconsToPosition to position of distribute
				else
					set end of ReturnIconsToPosition to desktop position of distribute
				end if
			end repeat
			if exists window "Desktop" then close window "Desktop"
		end try
	end tell
end GetPositions

on WritePositions()
	my SetPath()
	set fRef to (open for access file theStoragePath with write permission)
	try
		set TheWriteList to {}
		set eof fRef to 0
		set i to 0
		repeat with theItem in thelist
			set i to i + 1
			set the TheWriteList to TheWriteList & (theItem as string) & item 1 of item i of ReturnIconsToPosition & item 2 of item i of ReturnIconsToPosition
		end repeat
		write TheWriteList to fRef
	end try
	close access fRef
	display dialog "The Icon Positions have been saved." buttons {"OK"} giving up after 4
end WritePositions

on ReadOldPositions()
	set thelist to {}
	set ReturnIconsToPosition to {}
	set temp to {}
	set WholeList to {}
	my SetPath()
	tell application "Finder"
		try
			set WholeList to (read file (theStoragePath) as list)
		on error
			display dialog "There does not seem to be any positions saved." buttons "Cancel"
		end try
	end tell
	set i to 1
	repeat until i > (number of items in WholeList)
		set end of thelist to item i of WholeList
		set temp1 to {item (i + 1) of WholeList as number}
		set temp2 to {item (i + 2) of WholeList as number}
		set temp to temp1 & temp2
		set end of ReturnIconsToPosition to temp
		set i to i + 3
	end repeat
end ReadOldPositions

on ReplacePositions()
	tell application "Finder"
		try
			set i to 0
			set IconsReplaced to 0
			set IconsNotReplaced to 0
			repeat with distribute in thelist
				set i to i + 1
				if WindowExists then
					if item distribute exists then
						set position of item distribute to item i of ReturnIconsToPosition
						set IconsReplaced to IconsReplaced + 1
					else
						set IconsNotReplaced to IconsNotReplaced + 1
					end if
				else
					if item distribute exists then
						set desktop position of item distribute to item i of ReturnIconsToPosition
						set IconsReplaced to IconsReplaced + 1
					else
						set IconsNotReplaced to IconsNotReplaced + 1
					end if
				end if
			end repeat
		end try
		set TextString to "There were " & IconsReplaced & " Icons repositioned"
		if IconsReplaced = 0 then set TextString to "There were no Icons repositioned"
		if IconsReplaced = 1 then set TextString to "There was one Icon repositioned"
		if IconsNotReplaced = 0 then set TextString to TextString & "."
		if IconsNotReplaced = 1 then set TextString to TextString & ", and one was missing."
		if IconsNotReplaced > 1 then set TextString to TextString & ", and " & IconsNotReplaced & " were missing."
		display dialog TextString buttons "OK" giving up after 6
	end tell
end ReplacePositions

on SetPath()
	tell application "Finder"
		try
			if WindowExists then
				set thePath to (folder of the front window) as string
			else
				set thePath to (path to desktop folder as string)
			end if
		on error
			return false
		end try
	end tell
	set theStoragePath to thePath & "Icon Storage.dat"
end SetPath

on CheckIfFileExists()
	my SetPath()
	tell application "Finder"
		try
			set WholeList to (read file (theStoragePath) as list)
			return true
		on error
			return false
		end try
	end tell
end CheckIfFileExists

Model: G5 1.8 GHz
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Never mind, I’ve worked out that the script as an App is already selected itself, and this overrides the selection of a window from within the App script. (so, it only saves on position)

The question boils down to … is there any way of deselecting the App, so the window selection is used?

Regards

Santa

In similar situations, I either use Butler to assign a keyboard shortcut or control-drag the app into the Finder toolbar and launch it from there.

Thanks Capitalj

Unfortunately that works ¢provided¢ nothing is selected on the desktop, otherwise that desktop selection is ‘seen’ in preference to the window icon selection. :frowning:

My dilemma is how to either turn off the desktop selection, or ensure that the window selection takes precedence.

Santa

G’day again

What I think I need to do is send a click (or something) to the frontmost window, before selecting the icons in it, so that the desktop selection becomes deselected.

I’ve tried various bits of scripting, but haven’t succeeded. Is there such a procedure?

Regards

Santa

I was in the middle of reading (and rereading) the script, so this is off the top of my head, but what about telling Finder to open window 1?

Good Try Capitalj, but no go. The desktop selection still stays, and is referenced first. Bummer.

Santa

Hmm, then how about checking the list in the GetPosition handler and removing icons with incorrect paths?

j

When I say ‘referenced first’ I meant that it ¢only¢ looks at the desktop :slight_smile:

This procedure saves the Window icons.

I wait until the first dialog box opens, then click on the window, then proceed to choose the window from the dialog.

If I can just do the same with the script, I’m right I think. ¢if¢

Santa

Oops. :smiley: I understood that a few minutes ago, but it’s 3 a.m. so…

I tried a quick experiment - telling Finder to select item 1 of window one, hoping that would deselect the icon on the desktop, but it didn’t work.

But pressing the tab button deselects, so maybe a “keystroke tab” instead of (or in addition to) open window one.

Yer up late mate. it’s only 5 pm here in sunny ol’ Oz :cool:

I think Tab won’t work, as it selects the next icon on the desktop.

Santa

A bit of an insomniac. Nice and quiet right now, though.

:rolleyes: I randomly selected an icon and when I pressed tab the newly selected icon was obscured by a window. I don’t usually navigate that way. I may not be helping but at least I’m entertaining.

The escape key does deselect. If Finder is the frontmost app. I double checked. Don’t know the keycode for it. Maybe that will work. I’ll check back to see in the morning (your time.)

j

Edited for typos, again.

I ran the App, then selected the App so the dialog box was deselected, hit ESC and deselected the App, went back to thedialog box and chose ‘Window’, and the result was nothing was saved.

I really need to select that darn window somehow.

The following at least doesn’t crash, but does nothing. I’ll keep trying.

Santa


tell application "Finder"
	try
		set temp to name of the front window
		set WindowExists to true
		open window 1
		tell application "System Events"
			click {1, 1}
			tell window temp
				click {1, 1}
			end tell
		end tell
	on error
		set WindowExists to false
	end try
end tell

How about this?


tell application "Finder"
	try
		set temp to name of the front window
		set WindowExists to true
		select window of desktop
		
		tell application "System Events"
			tell process "Finder"
				set frontmost to true
				key code 53
			end tell
		end tell
	on error
		set WindowExists to false
	end try
end tell

I found the keycode here - http://bbs.applescript.net/viewtopic.php?id=14992 I didn’t try it in your full script.

j

G’day capitalj

That deselects the selection on the desktop just fine thanks, but it still leaves the frontmost window deselected, so no icons are seen.

I’ve tried sending a ‘return’ to the window, but no result. I think the process is still being focussed on the desktop.

I really need to send a mouse click to the window I think, but how?

Regards

Santa

Thanks Capitalj. I’ve worked it out. This script works once the desktop is deselected. That key code was the answer alright. :smiley:


tell application "Finder"
	try
		set temp to name of the front window
		set WindowExists to true
		select window of desktop
		tell application "System Events"
			tell process "Finder"
				set frontmost to true
				key code 53
			end tell
		end tell
		select window temp
		tell application "System Events"
			tell process "Restore Desktop Icons"
				set frontmost to true
			end tell
		end tell
	on error
		set WindowExists to false
	end try
end tell

Glad to here it - quite relieved actually. I made some silly suggestions and was hoping to redeem myself. :stuck_out_tongue: