Why Doesn't This Code Work?

Hello Shane.

The Finder has a “logical behavior”. If the ending substring “describes the contents of the file”, it IS a name extension. In the other case, ITSN’T such object.
What is the interest of the object “name extension” if it is not guaranteed to be a “contents descriptor” ?

My grand daughter urged the Finder to never display the name extension. She also urged it to display windows with icons and to build them on the fly according to file’s contents. The result is that she never know which kind of file is in front of her.

My daughter made a more ‘logical’ choice. She hide name extensions but she display windows in columns mode so she see the fil type in its dedicated column.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 24 avril 2020 11:32:27

But how does the Finder know? You have a file called La.guillotine. How does it know I didn’t save the file from my Chop.app and send it to you?

@Shane
I’m sure that you know that but some readers may ignore it.
The Finder maintain a database of every name extension defined by the applications installed on the running machine.
As long of your application is not installed, “guillotine” is not identified as a name extension.
Such infos are given by the info.plist of every application in the array Exported Type UTIs.
For instance, in Preview’s plist, the first item of the array is the dictionary Item 0 (public radiance).
This dictionary contains a dictionary named Equivalent Types which itself contain an array of strings which are some of the name extensions which may be given to files exported by the app.
Here they are “pic”, “PIC”, “hdr”, “HDR”
In your app it would be “guillotine” (with perhaps its Uppercase version “GUILLOTINE”)
Complementary detail, if we uninstall the application, its set of extensions remains in the Finder’s database (the launchservices one if I remember well).

As System Events and Foundation are too lazy to use the informations available to both of them, they make no difference between a registered/identified extension and a standard string.

Jean-Paul Sartre wrote a play entitled “La P… respectueuse” (when he did we didn’t use ellipsis but 3 dots and France was ridiculous enough to refuse to write the word “Putain”)

If I saved a text file containing this play in old times, the file would have been named “La P… respectueuse” because in these old times Apple system didn’t use name extensions.
I have a lot of such old files and I never spent time to give them an extension.
As far as I know, nobody created an application using “respectueuse” as a name extension so, the Finder would be fair enough to know that the file has no extension.
The other tools, which may perfectly grab the info are too lazy to do that and would state that the file has the name extension “respectueuse” which is perfectly ridiculous and just prove their infamous laziness.

@KniazidisR
I apologize but I can’t check what you wrote.
I tried to urge the Finder to stop displaying name extensions.
I entered its Preferences pane and unchecked the dedicated checkbox and for safe, I rebooted
No luck, extensions were always displayed.
I entered the file com.apple.finder.plist and saw that the property AppleShowAllExtensions was set to YES. I edited it as NO, rebooted and extensions were always visibe.

The script below extracts the available settings and you will see that the results match what I wrote.

tell application "System Events" to tell process "Finder"
	set frontmost to true
	keystroke "," using {command down}
	class of UI elements --> {window, menu bar, scroll area}
	name of windows --> {"Préférences du Finder"}
	tell window 1
		class of UI elements --> {checkbox, checkbox, checkbox, checkbox, checkbox, checkbox, static text, pop up button, button, button, button, toolbar, static text}
		tell toolbar 1
			class of UI elements --> {button, button, button, button}
			name of buttons --> {"Général", "Tags", "Barre latérale", "Options avancées"}
			click button -1 --> "Options avancées"
		end tell --> toolbar 1
		class of UI elements --> {checkbox, checkbox, checkbox, checkbox, checkbox, checkbox, static text, pop up button, button, button, button, toolbar, static text}
		title of checkbox 1 --> "Afficher toutes les extensions de fichiers"
		value of checkbox 1 --> 0 -- unchecked
		title of checkbox 4 --> "Avertir avant de vider la corbeille"
		value of checkbox 4 --> 1 -- checked
	end tell
end tell

set showExtensions to do shell script "defaults read com.apple.finder AppleShowAllExtensions"--> "0"

If I ask the Finder for the properties of a file, I get something like:

{class:document file, name:"|ContentsStoreFormat%1$@_loc|.txt", index:263, displayed name:"|ContentsStoreFormat%1$@_loc|.txt", name extension:"txt", extension hidden:false, container:folder "Desktop" of folder "**********" of folder "Users" of startup disk of application "Finder", disk:startup disk of application "Finder", position:{-1, -1}, desktop position:{1134, 768}, bounds:{-33, -33, 31, 31}, kind:"Document format texte", label index:0, locked:false, description:missing value, comment:"", size:674, physical size:4096, creation date:date "samedi 11 avril 2020 à  12:22:33", modification date:date "samedi 11 avril 2020 à  12:22:33", icon:missing value, URL:"file:///Users/**********/Desktop/%7CContentsStoreFormat%251$@_loc%7C.txt", owner:"**********", group:"(inconnu)", owner privileges:read write, group privileges:read only, everyones privileges:read only, file type:missing value, creator type:missing value, stationery:false, product version:"", version:""}

I may live with a system behaving this way but if you tell me what I must do to really stop the extensions display I would be glad to check how it behaves at script level.
Of course, after doing that I would return to my standard settings.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 24 avril 2020 15:11:06

This point has been made above, but I ran a simple test just to confirm my understanding and thought I would report the results.

I created two files with the following names: “Test File.txt” and “Test File.xxx”. I then ran the following scripts and received the results shown:

tell application "Finder"
	name extension of file "Macintosh HD:Users:Robert:Test File.txt" --> "txt"
	name extension of file "Macintosh HD:Users:Robert:Test File.xxx" --> ""
end tell

tell application "System Events"
	name extension of file "Macintosh HD:Users:Robert:Test File.txt" --> "txt"
	name extension of file "Macintosh HD:Users:Robert:Test File.xxx" --> "xxx"
end tell

So, at least in this case, Finder was able to distinguish an invalid file extension and System Events was not. I ran these tests twice–first with the file extensions visible in the Finder and then with the file extensions hidden. The results did not change from those shown above.

I discovered a funny thing.
Most of my files appear with their name extension but some don’t.
In a given folder
16 .numbers doc have their extension, 4 haven’t
2 .pages documents have their extension, 3 haven’t
20 applets/droplets have no extension
every pdf or rtf files have their extension
50 .scpt files have their extension, one hasn’t
15 .applescript files have their extension, one hasn’t

I can’t guess what is the logic at work in such behavior.
As I’m curious I ran a test file.

set aFile to (choose file) as alias

tell application "Finder"
	properties of aFile --> {class:document file, name:"Chèques YK 2019.numbers", index:9, displayed name:"Chèques YK 2019", name extension:"numbers", extension hidden:true, container:folder "ƒ banque" of folder "Important" of folder "Documents" of folder "**********" of folder "Users" of startup disk of application "Finder", disk:startup disk of application "Finder", position:{592, 22}, desktop position:missing value, bounds:{560, -10, 624, 54}, kind:"Feuille de calcul Numbers", label index:0, locked:false, description:missing value, comment:"", size:582932, physical size:618496, creation date:date "lundi 13 mai 2019 à  15:59:54", modification date:date "vendredi 24 avril 2020 à  15:44:00", icon:missing value, URL:"file:///Users/**********/Documents/Important/%C6%92%20banque/Che%CC%80ques%20YK%202019.numbers", owner:"**********", group:"(inconnu)", owner privileges:read write, group privileges:read only, everyones privileges:read only, file type:missing value, creator type:missing value, stationery:false, product version:"", version:""}
end tell

And what I see doesn’t match KniazidisR’s statements (but am’I really surprised by that?).
The name of the file has its extension “numbers”.
The extension “numbers” is perfectly returned.
The extension is described as hidden.
The displayed name is correctly deprived of the extension.

I ran an expanded version of the KniazidisR’s script:


property openTypes : {"PDF", "com.adobe.pdf", "BMP", "com.microsoft.bmp", "PICT", "com.apple.pict", "PNG", "public.png", "PSD", "com.adobe.photoshop-image", "TIFF", "public.tiff"}

--Get the artwork file
set theFiles to choose file with prompt "Choose art file(s)" of type openTypes with multiple selections allowed without invisibles
set theType to "JPG"

repeat with aFile in theFiles
	runTest(aFile, theType)
end repeat

on runTest(theArt, theType)
	log "theArt is " & theArt
	tell application "Finder"
		set theFolder to (container of theArt) as text
		set visiName to displayed name of theArt
		set theNewArt to theFolder & (name of theArt) & "." & theType
	end tell
	log "visible name is " & visiName & ", theNewArt is " & theNewArt & linefeed
end runTest

and what I got doesn’t match KniazidisR’s statements.
Note that the 1st and 3rd items have their extension visible, the 2nd one has its extension hidden although the 3 aliases appear with their extension.

tell application "Script Editor"
	choose file with prompt "Choose art file(s)" of type {"PDF", "com.adobe.pdf", "BMP", "com.microsoft.bmp", "PICT", "com.apple.pict", "PNG", "public.png", "PSD", "com.adobe.photoshop-image", "TIFF", "public.tiff"} with multiple selections allowed without invisibles
		--> {alias "SSD 1000:Users:**********:Documents:Important:ƒ banque:2018-11-23 Taxe habitation Roquesteron.pdf", alias "SSD 1000:Users:**********:Documents:Important:ƒ banque:2018-12-13.pdf", alias "SSD 1000:Users:**********:Documents:Important:ƒ banque:attestation tva 10%.pdf"}
end tell

(*theArt is SSD 1000:Users:**********:Documents:Important:ƒ banque:2018-11-23 Taxe habitation Roquesteron.pdf*)
tell application "Finder"
	get container of alias "SSD 1000:Users:**********:Documents:Important:ƒ banque:2018-11-23 Taxe habitation Roquesteron.pdf"
		--> "SSD 1000:Users:**********:Documents:Important:ƒ banque:"
	get displayed name of alias "SSD 1000:Users:**********:Documents:Important:ƒ banque:2018-11-23 Taxe habitation Roquesteron.pdf"
		--> "2018-11-23 Taxe habitation Roquesteron.pdf"
	get name of alias "SSD 1000:Users:**********:Documents:Important:ƒ banque:2018-11-23 Taxe habitation Roquesteron.pdf"
		--> "2018-11-23 Taxe habitation Roquesteron.pdf"
end tell
(*visible name is 2018-11-23 Taxe habitation Roquesteron.pdf, theNewArt is SSD 1000:Users:**********:Documents:Important:ƒ banque:2018-11-23 Taxe habitation Roquesteron.pdf.JPG
*)   WHICH IS WRONG

(*theArt is SSD 1000:Users:**********:Documents:Important:ƒ banque:2018-12-13.pdf*)
tell application "Finder"
	get container of alias "SSD 1000:Users:**********:Documents:Important:ƒ banque:2018-12-13.pdf"
		--> "SSD 1000:Users:**********:Documents:Important:ƒ banque:"
	get displayed name of alias "SSD 1000:Users:**********:Documents:Important:ƒ banque:2018-12-13.pdf"
		--> "2018-12-13"
	get name of alias "SSD 1000:Users:**********:Documents:Important:ƒ banque:2018-12-13.pdf"
		--> "2018-12-13.pdf"
end tell
(*visible name is 2018-12-13, theNewArt is SSD 1000:Users:**********:Documents:Important:ƒ banque:2018-12-13.pdf.JPG
*)  WHICH IS WRONG

(*theArt is SSD 1000:Users:**********:Documents:Important:ƒ banque:attestation tva 10%.pdf*)
tell application "Finder"
	get container of alias "SSD 1000:Users:**********:Documents:Important:ƒ banque:attestation tva 10%.pdf"
		--> "SSD 1000:Users:**********:Documents:Important:ƒ banque:"
	get displayed name of alias "SSD 1000:Users:**********:Documents:Important:ƒ banque:attestation tva 10%.pdf"
		--> "attestation tva 10%.pdf"
	get name of alias "SSD 1000:Users:**********:Documents:Important:ƒ banque:attestation tva 10%.pdf"
		--> "attestation tva 10%.pdf"
end tell
(*visible name is attestation tva 10%.pdf, theNewArt is SSD 1000:Users:**********:Documents:Important:ƒ banque:attestation tva 10%.pdf.JPG
*)  WHICH IS WRONG

Important feature, the authoring application name its documents and its windows according to the Finder’s displayed name.

set aFile to ((path to documents folder as string) & "Important:ƒ banque:Chèques YK 2019.numbers") as «class furl»
tell application "Numbers"
	open aFile
	name of window 1 --> "Chèques YK 2019"
	tell document 1
		its name --> "Chèques YK 2019"
		its file --> file "SSD 1000:Users:**********:Documents:Important:ƒ banque:Chèques YK 2019.numbers"
		its modified --> false
	end tell
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 24 avril 2020 16:52:26

I made a mistake in my testing. I tried giving the folder an extension and tried to trick Finder or System Events hiding the extension. So, I gave the wrong information - I “can deceive” System Events, not Finder. That is, the opposite is true. Just the System Events takes the text after the dot as name extension, is it hidden or now. Testing the same with file, both Finder and System Events returns trust result.

That is, it is better not to argue about name extensions with Finder. :rolleyes: Makes sense to delete my previous post so as not to mislead users anymore. It turns out that not this Finder behaviour is harmful to health, but namely my previous post…

Don’t forget I don’t apply this new name to the real file yet. So, it is true.

Yvan,

The database (part of Launch Services) is not maintained by the Finder – it is consulted by the Finder – that’s how it knows what application to use when you double-click on a document.

Your characterisation of other approaches as lazy is incorrect: it’s a deliberate policy. Extensions are defined variously in system documentation as that text that follows the last . in a file name. They are not limited to those registered in a user’s Launch Services database.