Search code examples
macosapplescriptfinder

applescript get rid of full path


I've got a choose file in my AppleScript. When I run the script and choose a file, the output is always the full file path with the file extension on the end. For example:

Macintosh HD:Developer:About Xcode.pdf

is what I don't want. I only want:

About Xcode


The below answer by Kassym Dorsel doesn't work when there is more than one . in it.

The below answer by Lri doesn't work with set x to choose file:

error "Can’t make quoted form of alias \"Macintosh HD:Applications:Firefox.app:\" into        type Unicode text." number -1700 from quoted form of alias "Macintosh HD:Applications:Firefox.app:" to Unicode text

Solution

  • You can use the Finder to manipulate the names of Finder items:

    choose file with prompt "Pick one"
    set filepath to result
    
    tell application "Finder" to set {dispName, nameExt, isHidden} to ¬
        the {displayed name, name extension, extension hidden} of the filepath
    
    
    if isHidden or nameExt is equal to "" then
        dispName
    else
        (characters 1 through (-2 - (count of nameExt)) of dispName) as text
    end if
    
    set baseName to result