Okay, this is driving me crazy…
Does anyone know how to get the filename of a file dropped into a script? What seems to me like the likeliest method yields an error “can’t get name of Alias path:path:file” Can anybody tell me what I’m doing wrong?
on open the_file
set the myvariable to the name of the_file as list
displaydialog(myvariable)
end open
I’m fairly new to this, and any help at all would be very appreciated.
Thanks
Okay, after probing the past posts some, I was able manage this:
on open the_file
set AppleScript's text item delimiters to {":"}
set myvariable to the_file as string
set myvariable to the last text item of myvariable
display dialog (myvariable)
end open
But surely there must be an easier, more intuitive way to do this, no?
Caught by the list reference trap, eh?
Dragging returns a list, even if there is only one file. When you try to use items in the list, some methods will return a reference (item 1 of {a, b, c}) while others will implicitly dereference the item. (a) cf:
Using a repeat with variable will dereference:
on open the_files
repeat with i from 1 to the count of the_files
tell application "Finder"
set myvariable to name of (item i of the_files)
end tell
display dialog "The name of the file is " & myvariable
end repeat
end open
while using a repeat with each item in the list will need an explicit dereference:
on open the_files
repeat with each_file in the_files
tell application "Finder"
set myvariable to name of (contents of each_file)
end tell
display dialog "The name of the file is " & myvariable
end repeat
end open
Oh, and also, (though I don’t know if we have your full script, you may be doing this) getting the name needs a Finder tell block, since name is a property of Finder items.