Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

KevinMSadler

macrumors newbie
Original poster
Nov 17, 2007
19
0
Hi Guys,

No programming experience since 1979!! Trying to use applescript to get the name and extension of a file which is selected in the finder.

This tiny script:
tell application "Finder"
set theFile to selection
display dialog theFile as text
end tell

Gives me a window with the full path to the file - the folder delimiters are colons. I cannot find any way of getting just the name and extension out of this.
If I use: set fileName as (name of selection) - I get an error
If I use: set fileName as (name of theFile) - I get an error

This should be trivial but I cannot for the life of me figure it out.

Any hints would be greatly appreciated.

Kev
 
You need to coerce the selection into an alias. This should work:

Code:
tell application "Finder"
	set fileAlias to the selection as alias
	set fileName to name of fileAlias
	set fileExtension to name extension of fileAlias
	display dialog fileName & return & fileExtension
end tell

mt
 
Trying to use applescript to get the name and extension of a file which is selected in the finder.

Code:
tell application "Finder"
	set theFile to selection
	display dialog theFile as text
end tell

The Finder's 'selection' property contains a list of references. Once you've retrieved that list, you need to process each of those references in turn:

Code:
tell application "Finder"
    set fileNames to {}
    set theItems to selection
    repeat with itemRef in theItems
        set end of fileNames to name of itemRef
    end repeat
end tell
fileNames

Or, if you're making an assumption that only one item is selected:

Code:
tell application "Finder"
    set theItems to selection
    -- Notes: The next line will error if no items are selected.
    -- If more than one item is selected, the rest are ignored.
    set fileName to name of item 1 of theItems
end tell

HTH
 
Wow that was quick!

Thanks so much guys - the filename that is returned for both of your scripts includes the extension. Is there any way to return the filename stripped of the extension? (you have given me the way to extract the extension itself).

Thanks for the help

Kev
 
Thanks so much guys - the filename that is returned for both of your scripts includes the extension. Is there any way to return the filename stripped of the extension? (you have given me the way to extract the extension itself).

One (slightly crude) solution:

Code:
set fileName to "foo.bar.txt"
set AppleScript's text item delimiters to "."
if fileName contains "." then
    set {displayName, nameExt} to {text 1 thru text item -2, text item -1} of fileName
else
    set {displayName, nameExt} to {fileName, ""}
end if

One or two caveats with that code: it may mess up on hidden files whose names start with "." (but since Finder normally ignores hidden files that's probably not an issue), and the 'text item delimiters' property is global so if you perform other TIDs-related tasks later on (e.g. coercing a list of strings into a single string) then don't forget to (re)set the TIDs property first.

Mastering AppleScript's text item delimiters is a somewhat longer story (modesty prevents me mentioning the obvious:) but that should get you started.
 
Awesome

Thank you so much - it does exactly what I need. I can use this to feed into Hazel for more complex file renaming situations.

It is quite clear from the code what you are doing but I could not have written this myself. I searched through Apple's support site but could not find much to help me. Where do you suggest I go for a more complete understanding of text delimiters and of AppleScript in general?

All your help is hugely appreciated!

Kev
 
Thank you so much - it does exactly what I need. I can use this to feed into Hazel for more complex file renaming situations.

It is quite clear from the code what you are doing but I could not have written this myself. I searched through Apple's support site but could not find much to help me. Where do you suggest I go for a more complete understanding of text delimiters and of AppleScript in general?

Yeah, AppleScript code is very easy to read, even if you're not familiar with the language, but learning to write it is just as hard (and in some ways harder) than other languages.

There isn't a great deal of AS documentation on Apple's site, unfortunately. You'll find the AppleScript Language Guide by searching on developer.apple.com, but that's really just a reference list of the features provided by AS, and doesn't teach you much about how to use them effectively. To find your footing, consider investing in a good book on the subject - there are several options available (including the one in my sig, which I recently co-wrote /self-plug).

Beyond that, MacScripter is great for finding sample scripts and discussions when you're trying to solve a specific problem or learn your way around a particular application. And some apps, e.g. iTunes, have sites dedicated to them.
 
MacScripter is a great site, but you also can get most of your questions answered here at MacRumors as well. There are several regulars who can give good, comprehensive answers to just about any AppleScript question anyone would have.

mt
 
To find your footing, consider investing in a good book on the subject - there are several options available (including the one in my sig, which I recently co-wrote /self-plug).

Thanks very much for your help - just added your book to my Amazon UK wish-list. Will buy it as soon as I can.

Kev
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.