AppleScript to get file name

Discussion in 'Mac Programming' started by KevinMSadler, Jun 3, 2010.

  1. KevinMSadler macrumors newbie

    Joined:
    Nov 17, 2007
    #1
    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
     
  2. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #2
    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
     
  3. hhas macrumors regular

    Joined:
    Oct 15, 2007
    #3
    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
     
  4. KevinMSadler thread starter macrumors newbie

    Joined:
    Nov 17, 2007
    #4
    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
     
  5. hhas macrumors regular

    Joined:
    Oct 15, 2007
    #5
    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.
     
  6. KevinMSadler thread starter macrumors newbie

    Joined:
    Nov 17, 2007
    #6
    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
     
  7. hhas macrumors regular

    Joined:
    Oct 15, 2007
    #7
    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.
     
  8. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #8
    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
     
  9. KevinMSadler thread starter macrumors newbie

    Joined:
    Nov 17, 2007
    #9
    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
     

Share This Page