Get version of file into script and paste into Illustrator slug

I’ve put together a script (with the help of previous posters) to fill in the information in a slug in Illustrator. It all works perfectly apart from the section that gets the file version. The section is commented in the script. This works if you run the script more than once on any particular document!

Any ideas?

The full version of the script posted here will put information into named boxes in and illustrator document. But to see the error you only need to look in the Events and Replies of Applescript Editor and see that the information does not go in first time round.

-- captures the user's first name from the system for use in dialog boxes
set myName to (long user name of (system info))
set sp to (offset of " " in myName)
set myFirstName to text 1 thru (sp - 1) of myName

-- captures the user's short name from the system for use in the slug
set myShortName to (short user name of (system info))
set myInitials to text 1 thru 2 of myShortName

tell application "Adobe Illustrator" to set myDocCount to count of documents
if myDocCount > 0 then
	tell application "Adobe Illustrator"
		try
			set myPath to file path of document 1 as alias -- if the document is not saved this will cause an error
			
		on error
			display dialog "You haven't saved document yet " & myFirstName & "." buttons {"Cancel", "Save"} default button 2
			
			-- brings up Save As dialog box
			tell application "System Events" to tell process "Adobe Illustrator"
				click menu item "Save As..." of menu 1 of menu bar item "File" of menu bar 1
				tell application "Finder"
					set filesavepath to "/Volumes/Acorn Studio/Client WIP"
				end tell
				delay 0.2
				tell window "Save As"
					keystroke "g" using {command down, shift down}
					repeat until exists sheet 1
						delay 0.5
					end repeat
					tell sheet 1
						keystroke filesavepath
						click button "Go"
					end tell
				end tell
			end tell
			-- end Save As dialog box
			
			return
			
		end try
		
		set myFile to name of document 1
		set myFolder to myPath as string
		
		-- this section to get the version of the file causes an error first time round
		
		--try
		tell application "Finder"
			set myFileVersion to version of myPath as text
		end tell
		set AppleScript's text item delimiters to "Created"
		set myFileVersion to text item 1 of myFileVersion
		
		if myFileVersion is "Saved As v.14 " then
			set myFileVersion to "Illustrator CS4"
		else if myFileVersion is "Saved As v.13 " then
			set myFileVersion to "Illustrator CS3"
		else if myFileVersion is "Saved As v.12 " then
			set myFileVersion to "Illustrator CS2"
		end if
		set AppleScript's text item delimiters to {""}
		--end try
		
		-- end of error section
		
		set AppleScript's text item delimiters to ":"
		set clientFolder to text item 3 of myFolder -- always picks out the client folder provided the file is saved in Client WIP
		set AppleScript's text item delimiters to {""}
		if clientFolder contains "_" then
			set clientFolder to my cleanName(clientFolder) -- to format the client names properly
		else
			set clientFolder to clientFolder
		end if
		
		tell application "Finder"
			set myDate to (current date) as string
		end tell
		
		set myDate to (word 2 of myDate) & " " & (text 1 thru 3 of word 3 of myDate) & " " & (word 4 of myDate) as string
		
		set myRevised to myDate -- temporary until old artwork slugs are out of the system
		
		-- checks for the correctly named frames and then populates with the correct information
		if exists (text frame "titleblock-software" of document 1) then
			set contents of text frame "titleblock-software" of document 1 to myFileVersion
		end if
		if exists (text frame "titleblock-client" of document 1) then
			set contents of text frame "titleblock-client" of document 1 to clientFolder
		end if
		if exists (text frame "titleblock-file" of document 1) then
			set contents of text frame "titleblock-file" of document 1 to myFile
		end if
		if exists (text frame "titleblock-path" of document 1) then
			set contents of text frame "titleblock-path" of document 1 to myPath as string
		end if
		if exists (text frame "titleblock-date" of document 1) then -- the aim is to use this only eventually
			set contents of text frame "titleblock-date" of document 1 to myDate
		end if
		if exists (text frame "titleblock-revised" of document 1) then -- temporary until old artwork slugs are out of the system
			set contents of text frame "titleblock-revised" of document 1 to myRevised
		end if
		if exists (text frame "titleblock-editor" of document 1) then
			set contents of text frame "titleblock-editor" of document 1 to myInitials
		end if
		if exists (text frame "titleblock-version" of document 1) then
			set versionCount to get contents of text frame "titleblock-version" of document 1
			set versionCount to (versionCount + 1)
			set contents of text frame "titleblock-version" of document 1 to versionCount
			
			-- end populating named frames
		end if
		
	end tell
end if

-- this handler converts every 'odd' character to an underscore, modify as needed
on cleanName(newName)
	set chars to every character of newName
	repeat with i from 1 to length of chars
		if item i of chars as text is equal to "_" then
			set item i of chars to " "
		end if
	end repeat
	return every item of chars as string
end cleanName

Model: 2 x 2.8 GHz Quad-Core Intel Xeon
AppleScript: 2.1.2
Browser: Firefox 3.6.14
Operating System: Mac OS X (10.6)

Hi,

I’m also getting inconsistent results from the Finder.

Try this method, it reads the contents of the file and returns the CreatorVersion

Add this handler somewhere

on getVersion(added_item)
	set itemVersion to do shell script "tr \"\\r\" \"\\n\" <" & quoted form of POSIX path of added_item & " | grep -a -m1 AI8_CreatorVersion"
	set myFileVersion to text ((offset of "%%AI8_CreatorVersion:" in itemVersion) + 22) thru end of itemVersion
end getVersion

and call it with

set myFileVersion to my getVersion(myPath)
		if myFileVersion contains "14" then
			set myFileVersion to "Illustrator CS4"
		else if myFileVersion contains "13" then
			set myFileVersion to "Illustrator CS3"
		else if myFileVersion contains "12" then
			set myFileVersion to "Illustrator CS2"
		end if

Hope that helps

Ian

Hi Ian

Thanks for your response which works fine. The only problem is that I really need to get the file version rather than the creator version. ie We sometimes work in CS4 and save back to CS3. Any ideas how to do that with the shell script?

Cheers
Dave

Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)

Hi,

Sorry didn’t realise that in the original post.

on getVersion(added_item)
	set itemVersion to do shell script "tr \"\\r\" \"\\n\" <" & quoted form of POSIX path of added_item & " | grep -a -m1 %%Creator:"
	set myFileVersion to text ((offset of "%%Creator:" in itemVersion) + 11) thru end of itemVersion
end getVersion

Looking at it again, I think your original approach is OK, with 2 changes:
I have found the Finder to be very unreliable in Snow Leopard (scripting wise) so would get the long version from the file info (can be done without the finder)
The approach of using a handler when in an Illustrator tell block seems to work better

on getVersion(added_item)
	set myFileVersion to long version of (info for added_item)
end getVersion

then revert back to what you had

set myFileVersion to my getVersion(myPath)
		set AppleScript's text item delimiters to "Created"
		set myFileVersion to text item 1 of myFileVersion
		if myFileVersion contains "14" then
			set myFileVersion to "Illustrator CS4"
		else if myFileVersion contains "13" then
			set myFileVersion to "Illustrator CS3"
		else if myFileVersion contains "12" then
			set myFileVersion to "Illustrator CS2"
		end if
		set AppleScript's text item delimiters to {""}

Ian

Thanks very much for your help Ian, that works a treat.

Dave

Hi,

This script is great!

I am looking for a script that will put the Illustrator version in the comments field. I was to quickly look to see if the file is from CS6, CS5, etc.

I am a freelance designer that works with clients that have various versions of software and I want to quickly see if I saved my files back to their version.

Ideally, I would love for this to work on all programs but especially Indesign and Illustrator.

Thanks for any help you can offer.

Cindy in Indy

ok, I am an idiot. What I want is available as a view option. :smiley: