File name

I know this is something obvious… this works fine on my G4 at work:

on open docList
	set x to 1
	repeat number of items in docList times
		
		tell application "Finder"
			set theFile to item x of docList
			set theName to name of theFile
.... (and do things to the file name)

  • but on my iMac at home it doesn’t understand the ‘name of’ property. I think there is some scripting addition I have at work but not at home, but can’t locate it. It must have come with 10.4.6, but I’m running that in both locations.

Thanks for any help!
Emma
Leeds, UK

Try something simpler, EmmaG;

Does this work on the reluctant machine?

set f to choose file
tell application "Finder" to set theName to name of f

Yes Adam, it does! Thanks! … but why??

Something screwy about item x of doclist. If it’s not an alias try ‘set theFile to (item x of dockList) as alias’

How are you using your open handler, Emma?

A script that contains an open handler is usually intended to be saved as an application. The presence of the open handler will actually cause it to to be saved as a droplet. Whenever you drag items to the droplet in Finder, the item[s] dropped will be passed to the open handler, as a list of alias[es], for processing.

The choose file (or choose folder) command also returns an alias of the item chosen - and, from your test above, it’s clear that Finder on your machine still knows how to get the name of an alias (as, indeed, it should).

We can also use choose file to test your open handler - by presenting it with an alias (in a single-item list):

on open docList
	set x to 1
	repeat number of items in docList times
		tell application "Finder"
			set theFile to item x of docList
			set theName to name of theFile
			display dialog "NAME:" & return & theName (* test only *)
		end tell
		set x to x + 1 (* assumed as part of the original script, to increment the value of x *)
	end repeat
end open

open {choose file} (* this will pass an alias, as a single-item list, to the open handler *)

Of course, if the handler receives a list of unexpected items (such as strings, for example), an error will occur:

on open docList
	set x to 1
	repeat number of items in docList times
		tell application "Finder"
			set theFile to item x of docList
			set theName to name of theFile
			display dialog "NAME:" & return & theName (* test only *)
		end tell
		set x to x + 1 (* assumed as part of the original script, to increment the value of x *)
	end repeat
end open

open {"some file name or path"} (* this will fail because the list does not contain aliases *)
--> error number -1728 [errAENoSuchObject]: Can't get name of "some file name or path".

Incidentally, you might like to try an alternative to the set x/increment x approach - since AppleScript provides a slightly simpler method when using repeat loops. Here’s an example:

on open docList
	repeat with x from 1 to count docList
		tell application "Finder"
			set theFile to item x of docList
			set theName to name of theFile
			display dialog "NAME:" & return & theName (* test only *)
		end tell
	end repeat
end open

Hope that helps. :slight_smile:

1 Like