Newbie help with error

This is my first AppleScript. To my eyes, it looks fine, but I can’t figure out what the problem could be. My goal is for the script to look a few files, and if they exist, delete them (or in one case, the parent folder). Then it runs a program.

Here’s the script:

When I run it with the Editor, I get this error:

When I save as an application and run it, I get this error:


I’ve looked for the file in question, and it IS there. Removing that line doesn’t help… I get the same error on the next line.

Incidentally, the program I am trying to run in the script is a Classic application, but I’m editing with the OSX AppleScript Editor.

Thanks for your help!

Does this work?

set sd to (path to startup disk) as text

tell application "Finder"
	if exists alias (sd & "System Folder:Preferences:ACI:PIMScp.4DC.rex") then
		delete alias (sd & "System Folder:Preferences:ACI:PIMScp.4DC.rex")
	end if
	
	if exists alias (sd & "System Folder:Preferences:P.I.M.S.™ C/S Preferences:Terminal.PREFS") then
		delete alias (sd & "System Folder:Preferences:P.I.M.S.™ C/S Preferences")
	end if
	
	if exists alias (sd & "System Folder:Preferences:TCP.pref") then
		delete alias (sd & "System Folder:Preferences:TCP.pref")
	end if
	
	open file "EDC CLIENT"
end tell

– Rob

Right, you can’t refer to files like this. You are providing only a partial path. Here are several alternatives:


tell application "Finder"

	--		First, you can hard-code the startup disk:
	--
	set prefsFolder to folder "Mac HD:System Folder:Preferences:"
	--
	--	In the above, we provide a complete path-string, preceeded by
	--	the word "folder". This will turn the path-string into a
	--	Finder object reference:
	--
	--> folder "Preferences" of folder "System Folder" of startup disk of application "Finder"

	--		If you don't want to hard-code the volume name, then we can find it:
	--
	set volumeName to name of startup disk --> "Mac HD"
	--
	set prefsFolder to folder (volumeName & ":System Folder:Preferences:")
	--
	--	Again, we've created a complete path-string, and preceeded it by
	--	the word "folder". We could also use "item":
	--
	set prefsFolder to item (volumeName & ":System Folder:Preferences:")
	--
	--	which does the same thing, but it works for both files and folders

	--	Now another thing we can do is specify a folder "NAME" of folder:
	--
	if (exists folder "ACI" of prefsFolder) then

		set aciFolder to folder "ACI" of prefsFolder

		if (exists file "PIMScp.4DC.rex" of aciFolder) then

			-- do stuff

		end if
	end if
end tell

There are a multitude of ways to work with files under OS X. Most applications expect to work with an “alias”:

tell application "Not The Finder"
	set myFile to alias "Mac HD:myFolder:myFile"

The Finder itself preferes to work with Finder object references:

tell application "Finder"
	set myFile to file "myFile" of folder "myFolder" of startup disk

One frequently need to coerce between the 2 types:


	set myAlias to choose file --> returns an alias

	tell application "Finder"
		set myFinderObject to item myAlias -- object reference
		set backToAlias to myFinderObject as alias

Hi,

Also, the main thing is that ‘exists’ is a Finder command so it needed to be used in the Finder tell block. using mixed path reference and Finder reference with the Finder ‘startup dik’ property should work. Here I like to put the command in the beginning but your preference should work also:

tell application “Finder”
if exists (file “System Folder:Scrapbook File” of startup disk) then
beep 2
end if
if exists (file “System Folder:Clipboard” of startup disk) then
beep 2
end if
end tell

gl,

Oh my goodness gracious. I am such a complete and total idiot. How is it that after 8 years of AppleScripting that I should be unaware that references like the above work within the Finder?

Thanks Kel. I think I’m going to pop over to the AppleScriptSourceBook.com for a refresher course in telling the Finder what to do… :frowning:

Thanks for the help, folks. I haven’t had a chance to sit down and try them out yet, but I hope to do so tomorrow morning.

Arthur, thanks for the link to AppleScriptSourceBook.com. I have a lot of reading to do!

Keith

Hi,

I’ve been doing some rereading myself and can anybody tell me the difference between tell blocks and references to objects.

script ChildOfFinder
property parent : application “Finder”
on exists the_param
continue exists the_param
return result
end exists
end script

set sd to (path to startup disk) as string
set file_spec to (sd & “Installer Log File”) as file specification
tell ChildOfFinder
exists file_spec
end tell
{result, exists file_spec of ChildOfFinder}

What’s the difference between:

tell ChildOfFinder
exists file_spec
end tell
– result → true

and

exists file_spec of ChildOfFinder
– result → false

I must have read that section on script objects in the AppleScriptLanguageGuide.pdf a thousand times.

Thanks,

I’m not getting the same results you are. Using your code, I get false across the board (yes, I updated the code to a path to a file that actually exists). When I changed the code slightly to not use a file specification but a file reference, it returned true in all instances:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Thanks Jon. I’ve been getting a lot of different results like this with script objects running Jaguar. Here’s my log for the original script:

tell current application
path to startup disk
→ alias “Macintosh HD:”
end tell
tell application “Finder”
exists file “Macintosh HD:Installer Log File”
→ true
exists file_spec
→ false
end tell

Strange. I tried using the name reference and got true both times as you did. More to think about.

Thanks,