Adding an .ai Extension only to files that do not have one already

Hello,

I have some old files that no longer have the .ai extension and I have to rename each one in order to open the file. I could batch rename the files in their folders from Adobe Bridge, but then I would end up with 2 consecutive extensions ( .ai.ai), for those that already have them. I have been trying to piece together this script to add a .ai extension to only those files with no extension, but it is not even able to compile enough to test.

on open myFolder
repeat with i from 1 to length of myFolder
	my addMe(item i of theFolders)
end repeat
end
on addMe(thisFolder)
	try
		tell application "Finder" to set folderList to every item of thisFolder
		repeat with newFile in folderList
			if ((newFile as string) ends with " NOT SURE WHAT TO PUT HERE??") then
				addMe(newFile)
			else
				tell application "Finder" to set name of newFile to (((name of newFile) as string) & ".ai")
			end if
		end repeat
	 try 
end

I also don’t know what to place in the area that asks for “end with ?” in the script:
if ((newFile as string) ends with " NOT SURE WHAT TO PUT HERE??") then
addMe(newFile)

Thank you in advance for any help and guidance.
Best,
Babs

Try changing your if statement to the following:


if ((newFile as string) does not end with  ".ai" ) then

Hello Haolesurferdude,

Thank you for your reply.

I have been trying to figure this out adding your supplied lines, and have finally had some success in getting it at least to compile, but it doesn’t do anything when I run it?

Also, I am trying to get it to select a Folder and any Sub-Folder contents within that Folder and add the .ai to any file which does not have one.

Here is what I have this far:

on open myFolder
	repeat with i from 1 to length of myFolder
		my addMe(item i of theFolders)
	end repeat
end open
on addMe(thisFolder)
	try
		tell application "Finder"
			set chooseFile to choose file without invisibles
			
			set nameExtension to name extension of chooseFile
			display dialog nameExtension
			
			
			if ((newFile as string) ends with " NOT SURE WHAT TO PUT HERE??") then
				addMe(newFile)
			else
				tell application "Finder" to set name of newFile to (((name of newFile) as string) & ".ai")
			end if
		end tell
	end try
end addMe

thank you so much!
Babs

Model: MacBook Pro
AppleScript: 2.9
Browser: Safari 604.4.7
Operating System: Mac OS X (10.13 Developer Beta 3)

Hi. Even if your code worked, you’d spend all day with that handler because it invokes itself far more frequently than is reasonable—apparently for every identified file—and it presents a file chooser each time. Perhaps this alternate would work better; choose only a folder containing files known to need to end with the suffix, or a mess will be had.

property suffix : ".ai"
recurse(choose folder) 


on recurse(theFolder)
	tell application "Finder"
		repeat with targetFolder in (theFolder's folders as alias list)
			repeat with targetFile in (targetFolder's files as alias list)
				set isName to targetFile's name
				if isName does not end with suffix then set targetFile's name to isName & suffix
			end repeat
			my recurse(targetFolder)
		end repeat
	end tell
end recurse

Hi Marc,

Thank you for your insight and reply.

I tested your script and it does allow me to select a folder, but no suffixes get added.

I made a test folder with the following few files:

2 files that should be Adobe Illustrator Files, but without the suffix they will not open.
I also put a PDF and removed its suffix, to see what would happen, and there is a screenshot in my folder as well.
In addition, there are 2 legitimate .ai files in the folder, as I don’t want it to make a duplicate suffix.

So, in a nutshell, the script needs to find every image that should be an adobe illustrator file and add the .ai suffix, if one does not exist. But, it also needs to not create a suffix for other possible types. Not sure that is even remotely possible. But since the majority of these old folders probably only contain illustrator files I am not too worried.

Any thoughts???
thank you so much!!
Babs

I seem to recall—perhaps mistakenly—that a file’s kind may depend on its extension, so I’m not sure if the addition of that filter will work or not in the manner you intend. My original code worked on subfolders, but it neglected root level files. Try this edit:

property suffix : ".ai"
recurse(choose folder)


on recurse(theFolder)
	tell application "Finder"
				repeat with targetFile in (theFolder's files as alias list)
			set isName to targetFile's name
			if isName does not end with suffix and (get targetFile's kind) contains "Adobe Illustrator" then set targetFile's name to isName & suffix --questionably working
			--if isName does not end with suffix then set targetFile's name to isName & suffix --will work blindly
		end repeat
		repeat with targetFolder in (theFolder's folders as alias list)
			my recurse(targetFolder)
		end repeat
	end tell
end recurse

FWIW, I believe you’re correct. The only way, I suspect, is to read the first few bytes of each file to check. And that gets complicated because most .ai files begin the same as .pdf files (“%PDF-n.n”).

Hi…

Well, I tried the new script and the same thing. It runs, but doesn’t add the siuffix “.ai” to any file that has none.

Reading it makes sense that it should work, but not sure why nothing is being changed. I set my machine to show all suffixces and tried on both my local drive an the cloud server to see if that was the issue, but nothing seems to add the suffix. It looks like it should work?? Not sure what is causing it not to add it…

Any other thoughts? Thanks again for all the help!!
Babs

On my machine I have some files whose extension name is ai.
For see, I duplicated one of them and removed the extension.
When I asked Finder what is the kind of the file, it claimed → “Document SimpleText”
so it’s not surprising to read that the script didn’t add the the extension.

If I use the instruction :

if isName does not end with suffix then set targetFile's name to isName & suffix

the extension is correctly added but we have no guarantee that the file is correctly described.
When the extension is added, the kind is reported as “images”.

If I use System Events to grab descriptors, without the extension I get:
file type: “TEXT”
kind: “Document SimpleText”
creator type: “ART5”

with the extension I get :
file type: “TEXT”
kind: “images”
creator type: “ART5”
type identifier:“com.adobe.illustrator.ai-image”

So I decided to use brute force to get the identifier. I read the 255 first characters of the file to check if the block contains the string “Adobe Illustrator”

set suffix to ".ai"
set thePath to (path to desktop as text) & "Waffle Illusion copie"
set targetFile to thePath as alias
tell application "Finder"
	set isName to targetFile's name
	if isName does not end with suffix and my isItIllustrator(targetFile) is true then set targetFile's name to isName & suffix
	properties of targetFile
end tell

on isItIllustrator(aFile)
	set theBeg to read aFile for 255
	return theBeg contains "Adobe Illustrator"
end isItIllustrator

and the suffix was correctly added.

Yvan KOENIG running High Sierra 10.13.4 in French (VALLAURIS, France) lundi 16 avril 2018 17:44:39

That might be worth a try, given the OP is talking about old files, but it will fail with files saved from recent versions.

I’m wondering why nobody suggests to set the name extension rather than checking name ends with and concatenates name and extension.

tell application "Finder"
...
if name extension of aFile is not "ai" then set name extension of aFile to "ai"
...

Hi StefanK!

Thanks for the reply…
Put your part in and the errors I get look like this:

If there are files with extensions in the folder:
ERROR: Can’t get name extension of a "file name " with .png at the end.

If I get rid of those and just s leave the .ai files that do not have the extension, then I get this message:
ERROR:Can’t get name Extension of “name of a file”

Here is what I did with the information you added.

property suffix : ".ai"
recurse(choose folder)


on recurse(theFolder)
	tell application "Finder"
		repeat with targetFile in (theFolder's files as alias list)
			set aFile to targetFile's name
			if name extension of aFile is not "ai" then set name extension of aFile to "ai"
		end repeat
		repeat with targetFolder in (theFolder's folders as alias list)
			my recurse(targetFolder)
		end repeat
	end tell
end recurse

Thanks so much!
Babs

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

recurse(choose folder)

on recurse(theFolder)
	tell application "Finder"
		set theList to every file of theFolder as alias list
		repeat with aFile in theList
			if name extension of aFile is "" then set name extension of aFile to "ai"
		end repeat
	end tell
end recurse

Babs,

I hesitate to offer suggestions as my qualifications are extremely poor compared to those of the others who have replied, but I did have some time this afternoon and tried the following script on a folder with a few files in it. The script successfully added the “ai” extension to files of the selected folder that had no extension and did not add double “ai” extensions or change the extensions of files with other extensions in the folder. Hope I’m not just adding confusion to the situation.

Dana

No, the name of the file cannot be the file itself

repeat with targetFile in (theFolder's files as alias list)
           if name extension of targetFile is not "ai" then set name extension of targetFile to "ai"
...

Hi Dana and StefanK,

Dana
Your script worked great. The only thing missing was the ability to go into the subfolders, but I can certainly use your script on single folders that could have hundreds of missing suffixes, so thank you!

StefanK
Your addition has allowed me to access all the subfolders and no extra .ai files.
This was my final compiled script.

property suffix : ".ai"
recurse(choose folder)
on recurse(theFolder)
	tell application "Finder"
		repeat with targetFile in (theFolder's files as alias list)
			if name extension of targetFile is not "ai" then set name extension of targetFile to "ai"
		end repeat
		repeat with targetFolder in (theFolder's folders as alias list)
			my recurse(targetFolder)
		end repeat
	end tell
end recurse

So this might be all that is needed.
I will check tomorrow to see if there really are any valid concerns about the file type issue. I feel like it is only the very old illustrator files that lost their suffixes.
I have these 4 lines of code I have been trying to fit in to access the file type and isolate it, but did not have much luck. Hoping I don’t need them. These are the lines from Yvon:

on isItIllustrator(aFile)
set theBeg to read aFile for 255
return theBeg contains “Adobe Illustrator”
end isItIllustrator

I feel they would address the issue, but want to double check if they are needed.

I will respond back tomorrow once I know for sure.

Thank you ALL!!!
Babs

Thank you everyone!
The script is fine the way it works now.

Cheers!
Babs

In my opinion the number of characters you were reading is too small.
This seems to work in my hands. It identifies correctly Illustrator files.

set theFile to choose file
isItIllustrator(theFile)

on isItIllustrator(aFile)
	set theBeg to read aFile for 2550
	return theBeg contains "Adobe Illustrator"
end isItIllustrator

If you add this you can avoid adding “.ai” extension to files which are NOT Illustrator file.

Here is an enhanced version :

recurse(choose folder)
on recurse(theFolder)
	tell application "Finder"
		repeat with targetFile in (theFolder's files as alias list)
			if (name extension of targetFile is "") and my isItIllustrator(targetFile) then set name extension of targetFile to "ai"
		end repeat
		repeat with targetFolder in (theFolder's folders as alias list)
			my recurse(targetFolder)
		end repeat
	end tell
end recurse

on isItIllustrator(aFile)
	set theBeg to read aFile for 2550
	return theBeg contains "Adobe Illustrator"
end isItIllustrator

The old one added the extension “ai” to every file whose extension wasn’t already “ai”
Doing that it triggered the handler isItIllustrator (time consuming) for files having an extension like “.txt”.
If such a file - for instance “trucmuche.txt” contained the string “Adobe Illustrator” in its first 2550 bytes, it was renamed “trucmuche.ai” which is wrong.

Yvan KOENIG running High Sierra 10.13.4 in French (VALLAURIS, France) dimanche 22 avril 2018 15:19:47