Question

I want to get the name of the currently in the finder window selected file. I'm working under OS X 10.9.2.

Here's my code:

display dialog "test"
tell application "Finder"
  set theItems to selection
  display dialog number of theItems
  repeat with itemRef in theItems
    display dialog name of itemRef
  end repeat
end tell

In the Finder I selected only one mp3 file. If I run the script then the dialogbox with "test" and the dialogbox with "1" is displayed correctly. But then I got the error message that the file cannot be converted into a type string.

I hope you can help me to fix this bug and I thank you in advance for your reply!

Was it helpful?

Solution 2

file cannot be converted into a type string.

Then convert it to a string.

display dialog (name of itemRef) as string

I'd suggest you to use log instead of display dialog for debugging purpose.

For instance, log name of itemRef will return (in your "Events" and "Replies" window):

(name of document file 123.jpg of folder Desktop of folder Saturnix of folder Users of startup disk)

As you can see, this is much more complex than a simple string of test (like "123.jpg"). This is telling you that name of itemRef is not returning the actual name of the file itemRef but a reference to that name. Luckily enough, calling as string on that reference will return us the actual name of the file.

OTHER TIPS

The name property of a file is already a text (or string) type. You are not properly parenthesizing the dialog statement.

display dialog (get name of itemRef)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top