You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript "name extension" of file suddenly stopped working

Today an AppleScript function suddenly stopped working:


Asking the Finder for the name extension of a selected file has worked perfectly for years, but it now returns a null string. Yesterday I updated Xcode from 14.2 to 14.3, but it doesn't seem like that should have anything to do with it.


:-/


I'm running macOS 13.3 (22E252)


Any suggestions would be much appreciated.

iMac 27″, macOS 13.3

Posted on Apr 1, 2023 10:00 AM

Reply
Question marked as Top-ranking reply

Posted on Apr 2, 2023 11:34 AM

There is also the brute force Foundation approach:


use framework "Foundation"
use scripting additions

property ca : current application

set foo to ""

set f to POSIX path of (choose file of type "public.plain-text") as text
set fname to ca's NSString's stringWithString:f
set name_ext to fname's pathExtension()
set name_string to fname's lastPathComponent()

if (name_ext's isEqualToString:"txt") = true then
	set foo to (name_string as text)
end if

log (foo) as text


5 replies
Question marked as Top-ranking reply

Apr 2, 2023 11:34 AM in response to AdobeWanKenobe

There is also the brute force Foundation approach:


use framework "Foundation"
use scripting additions

property ca : current application

set foo to ""

set f to POSIX path of (choose file of type "public.plain-text") as text
set fname to ca's NSString's stringWithString:f
set name_ext to fname's pathExtension()
set name_string to fname's lastPathComponent()

if (name_ext's isEqualToString:"txt") = true then
	set foo to (name_string as text)
end if

log (foo) as text


Apr 2, 2023 4:32 AM in response to AdobeWanKenobe

As long as you have at least one file selected before you run the script, the code that you have shown, using the name extension, just works for me. In Script Debugger v8.0.5 and Apple's Script Editor. I would put a guard in against no selection… and I have taken some liberty regarding using a list and not the myFileName variable.


Tested: macOS 13.3.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set foo to {}

tell application "Finder"
	if not (get selection) is missing value then
		set sel to selection
	else
		return
	end if
	repeat with thefile in sel
		if name extension of thefile is "txt" then
			copy name of thefile to the end of foo
		end if
	end repeat
end tell
return



Apr 2, 2023 9:27 AM in response to VikingOSX

Thank you for testing this. I could step through the code and see that the file name was there, including the extension, but the property for the extension was "" (empty string). Capturing the last four characters of the file name [ text -4 thru -1 of (name of theFile as text) ] is working, so I'll leave it that way for now.


Four scripts that used that method had worked for years, but all failed at the same time. I can't help wondering if something in the recent Xcode update could have changed the way AppleScript interprets that in some systems? Quite a mystery. :-/

AppleScript "name extension" of file suddenly stopped working

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.