gadgetmo
gadgetmo

Reputation: 3172

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

Upvotes: 0

Views: 2609

Answers (2)

Kassym Dorsel
Kassym Dorsel

Reputation: 4843

This will work :

set a to "Macintosh HD:Developer:About.Xcode.pdf"
set text item delimiters to ":"
set temp to last text item of a
set text item delimiters to "."
set temp to text items 1 thru -2 of temp as text

Gives => About.Xcode

Upvotes: 0

Michael J. Barber
Michael J. Barber

Reputation: 25052

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

Upvotes: 1

Related Questions