Want to retrieve name extension from a file name or file path

I want to perform various auto-actions to files based on the file extension.

Why is the following code not working?

set fileTarget to (path to desktop folder as text) & "FilePorterPrefs.txt"
set theEXT to (name extension of fileTarget)

I get the following error

I worked on it all night, and researched a ton today, but can’t find this issue anywhere else.

Basically, the script performs actions on certain file types. If the file extension isn’t contained in my list, a choose from list dialog says, “What do you want to do with this file type?” & (file extension). Then it adds that name extension to my preference file for future use.

Hi Joey,

Looking at Standard Additions dictionary:

I don’t see desktop folder in there.

gl,
kel

Hi Joey,

Next, you can’t get ‘name extension’ without Finder or System Events.

Looking at the dictionary for System Events:

So, you need to place ‘name extension’ in a System Events tell block. That’'s the target.

tell app "System Events"
name extension of someFile
end tell

Edited: once you know how to look at the dictionaries, then it gets much easier. Also you need to know what the target of the commands are because they only work in there (the tell blocks or whatever). It’s better to use System Events than the Finder if you can do that.

Edited: here’s an excerpt from AppleScriptLanguageGuide.pdf:

gl,
kel

Thanks for the reply. It’s also not working in a “tell” statement. Essentially, I have

Mavericks:Users:joseph:Desktop:FilePorterPrefs.txt

and I want to harvest what the OS recognizes as the file extension. In this case, txt.

I’d post the whole script, but I’m not done with it (still need to be able to change a specified paragraph in a txt file to write to my prefs file). That’s why I posted that small little snippet.

You did give me an idea though. I got the name extension to work once, but then couldn’t get it working again later. Maybe I was within a tell block and didn’t think about it.

The quickest way is to use text item delimiters:

set thePath to "Mavericks:Users:joseph:Desktop:FilePorterPrefs.txt"
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
set theExt to last text item of thePath
set AppleScript's text item delimiters to saveTID

It assumes that there is an extension, but that’s usually a safe bet.

Hi Joey,

Try:

set fileTarget to ((path to desktop as string) & "hello.txt") as alias

The as alias makes it a reference and not just text. If the file doesn’t exist then it will error.

Edited: btw, you know you should replace the hello.txt with your own file name.

Edited: I didn’t see it earlier, but

this is saying that you’re using text and not a reference I think. The ‘as alias’ makes it a reference if the file exists. While at the same time you need to use it in the tell block.

gl,
kel

In a nutshell:

set fileTarget to ((path to desktop folder as text) & "FilePorterPrefs.txt") as alias
tell application "System Events"
	set theEXT to (name extension of fileTarget)
end tell

Sorry, I’ve not been feeling well.

gl,
kel

Thanks to both of you guys! That Tell thing got me looking a little closer at the script, and it was Tell app “Finder”.

Here’s what I ended up with:

set fileTarget to ((path to desktop folder as string) & "FilePorterPrefs.txt") as alias
tell application "Finder"
	set theEXT to (name extension of fileTarget)
end tell

Thanks again!

My best choice would be a slightly modified version of Shane’s one :

set thePath to (choose file) as text

set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
set theExt to last text item of thePath
set AppleScript's text item delimiters to saveTID
if theExt ends with ":" then set theExt to text 1 thru -2 of theExt
theExt

Yvan KOENIG (VALLAURIS, France) samedi 4 octobre 2014 13:11:46