Thursday, September 20, 2018

#1 2018-04-13 09:25:32 am

barbarapress
Member
Registered: 2005-05-04
Posts: 267

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.

Applescript:

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

Offline

 

#2 2018-04-13 01:58:32 pm

haolesurferdude
Member
Registered: 2010-02-19
Posts: 117

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

Try changing your if statement to the following:

Applescript:


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

Offline

 

#3 2018-04-15 07:45:48 am

barbarapress
Member
Registered: 2005-05-04
Posts: 267

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

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:

Applescript:

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)

Offline

 

#4 2018-04-15 01:13:24 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 819

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

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.


Applescript:

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

Last edited by Marc Anthony (2018-04-15 01:13:36 pm)

Offline

 

#5 2018-04-15 02:34:50 pm

barbarapress
Member
Registered: 2005-05-04
Posts: 267

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

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

Offline

 

#6 2018-04-15 07:29:49 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 819

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

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:

Applescript:

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

Offline

 

#7 2018-04-15 09:37:59 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5448

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

Marc Anthony wrote:

I seem to recall—perhaps mistakenly—that a file's kind may depend on its extension



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").

Last edited by Shane Stanley (2018-04-15 11:36:58 pm)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#8 2018-04-16 08:46:28 am

barbarapress
Member
Registered: 2005-05-04
Posts: 267

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

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

Offline

 

#9 2018-04-16 09:45:02 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3256

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

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 :

Applescript:

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"

Applescript:

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

Offline

 

#10 2018-04-16 11:27:16 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5448

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

Yvan Koenig wrote:

I read the 255 first characters of the file to check if the block contains the string "Adobe Illustrator"



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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#11 2018-04-17 01:48:42 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11508
Website

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

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

Applescript:

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


regards

Stefan

Offline

 

#12 2018-04-17 11:25:36 am

barbarapress
Member
Registered: 2005-05-04
Posts: 267

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

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.

Applescript:

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

Offline

 

#13 2018-04-17 03:18:06 pm

danaeugene
Member
Registered: 2017-04-02
Posts: 8

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

Applescript:

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

Offline

 

#14 2018-04-17 03:25:30 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11508
Website

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

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

Applescript:

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"
...


regards

Stefan

Offline

 

#15 2018-04-17 08:16:05 pm

barbarapress
Member
Registered: 2005-05-04
Posts: 267

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

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.

Applescript:

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

Offline

 

#16 2018-04-19 04:43:27 pm

barbarapress
Member
Registered: 2005-05-04
Posts: 267

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

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

Cheers!
Babs

Offline

 

#17 2018-04-20 03:21:49 am

ldicroce
Member
Registered: 2017-11-25
Posts: 50

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

barbarapress wrote:


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:

Applescript:

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.
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.

Applescript:

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.

Offline

 

#18 2018-04-22 07:20:00 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3256

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

Here is an enhanced version :

Applescript:

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)