script to add the correct pc extension to any file

Hi all,
we recently bought ourselves the new LaCie Tb ethernetdisk and it’s a charm. High Speed data and lots of storage. The only irritating thing is, it’s based on microcrap Windows XP. So it can not handle creator types. Instead it needs pc extensions to recognize files.

Of course not all our mac files are saved with the appropriate extensions. Some of them are 12 years old.

My question is: Is it possible to make an applescript that automatically adds the corresponding pc extension to a file based on it’s creator and type? Add the extension .eps to an EPSF file created with ART5 (i.e. illustrator)?
Some files however already have the correct extension since created in OSx.

But preferably automated for all kinds of files, jpeg, psd, tiff,doc,… etcetera. I’m looking at renaming 600gigs of files manually!!!

THX in advance

Floris

Someone has done this before, and as I recall, for the same reason. I don’t have time to search just now, but I think it is in this Forum.

Hi,
I have been searching the forum for days now and i have’nt found anything yet. Otherwise i wouldn’t bother posting my request. Thanks anyway for helping me out.

Here are a couple of examples:

http://bbs.applescript.net/viewtopic.php?pid=67656
http://bbs.applescript.net/viewtopic.php?pid=53938#p53938

I don’t know what to suggest with respect to a 12-year old file, but I would start by finding out what this revealed for you as you select some of your old files in a Finder window and run this:


tell application "Finder" to set FT to file type of (selection as alias)
display dialog FT

If the answers are not ‘missing value’ then you’ve got it made.

The trick is YOU need to know the File Type and File Creator in order to make the fixes. Mac OS X is kinda dumb on this front, I found much to my dismay.

We had a bunch of Mac files in a Digital Asset Manager (DAM) that handled Mac files fine. We had to migrate to a new system, and all seemed well until we realized far too late that all File Type and File Creator information got stripped. Lost the resource fork, essentially.

Since we couldn’t fix the hundreds of thousands of assets (nearly 1.2 TB) server-side, we opted for a “fixit” routine I wrote for Mac folks when they download the files. After the download they drag-n-drop the files onto my droplet and it fixes over 95% of issues, including restoring previews in image files. It also makes minor changes, like applying extensions to files that didn’t have them, or fixes extensions that the asset manager “gives” them because they had none (the DAM guesses wrong alot).

I did all this “magic” by scouring our Macs for every common file type the DAM contained, even scouring old archives, and then creating a chart of Type and Creator. This involved a script that would write to a text file the file name, Creator, and Type. Then I had to see how much of the Type/Creator info was unique to certain extensions (this gives the File Type part of the equation). I also had to use hexdumps to view file contents looking for unique strings (the Creator Type detective work). Then I had to reference that against things I could figure out from a file that didn’t have this information, which meant reading headers, knowing something about our internal file naming standards, etc.

In the end, I had 40 cases I had to do lookups against. I narrowed the list down to a tree of conditions. Sometimes a single condition defined a Creator/Type pair; sometimes it spawned a sub-tree of conditionals to check; sometimes a “this condition AND that condition” had to be met, which could then spawn more sub-conditions. In other words, nowhere near straightfoward.

In fact, last week we found a certain file with a certain “guess” induced by the DAM caused some files not to be “fixed” by my script. I narrowed down what the problem was (a set of circumstances I didn’t predict), and I will have to re-assess all my nested conditionals and rebuild one of the branches for PDF-vs-Illustrator detection.

One thing I learned, you can’t reply on the Mac OS itself. It is “guessing” based on the existing file extension. If none exists, it’s a horrible guesser. For example, Adobe Illustrator CS files look like PDFs, so the Mac OS can’t tell the difference between a “real” PDF and an Illustrator file without an extension. The only real way to tell is to GREP the contents of the file seeking specific text strings embedded therein. This is further complicated by “encrypted” files formats used by some companies (cough Quark XPress cough) that have no English-readable strings for GREP to find. Those were fun…NOT.

If anyone is interested, I can post the code I came-up with, but it’s pretty long. It’s not as elegant as it could be, and is very biased towards graphics files (I work in the Creative Services department). I’ve also since learned better ways to do things like file extension detection, I just don’t have the time to go back and re-write the appropriate handlers.

Here are some threads which may help. They were spawned by the script work I just mentioned above:

Reading Headers
http://bbs.applescript.net/viewtopic.php?id=17568

Managing Complex Conditional Testing
http://bbs.applescript.net/viewtopic.php?id=17812

I never posted my finished script, since I wasn’t sure if: a) there was interest, or b) where was an appropriate place to put it (since it’s final form was a FaceSpan application). The raw AppleScript from FaceSpan would probably work fine if you recognize all the “FaceSpan bits” and leave them out, for example.

hi kevin,

it would be great if you could post your code in ‘Code Exchange’ with the explaination you typed here (or something similar). i’m quite sure that:

a) this would be really useful to someone, and
b) the process of creating the script is facinating to me and others on the board.

seriously, it’s no wonder you are in creative services. your description reads like a book.

even if the user has to ‘glue the bits’ into a FaceSpan application (i don’t use FaceSpan myself), a good coder could take what you’ve done and make something that works for their particular situation. i’d be interested in examining how you’ve mapped the creator codes and extensions.

cheers.

… And I’m interested in how you’ve dealt with the huge collection of if, then, else…

Hello

Here is a script I often uses for similar task.

I hope that you will not be afraid by the french names used for variables.
Remember that variable’s name is just a label withoutout any meaning for the script.
As I often wrote, name a variable G_W_Busch, Charles_De_Gaulle or Fidel_Castro, the result will be the same :wink:


--[Script Réparer AW_docs]

(*
Remet, un couple type/créateur correct à des fichiers
documents AppleWorks 6 et ajoute si nécessaire le suffixe ".cwk"
Glisser les icônes des documents sur celle du script
ou choisir un dossier contenant les documents après avoir exécuté le script.

¢ Reset a correct pair Type/creator to AppleWorks files
and, if needed, add the suffix ".cwk".
Drop the documents's icons on the script's one
or choose a folder containing the documents after running the script.
  *)
property englishMsg : true
--  lignes exécutées si on double clique l'icône du script

property lesTypes : {"CWGR", "CWWP", "CWSS", "CWDB", "CWPT", "CWPR"}

property antislash : (ASCII character 92)
property signRTF : "{" & antislash & "rtf1" & antislash & "mac" & ¬
	antislash & "ansicpg10000" & antislash & "cocoartf102"
property signPDF : "%PDF-1."

property msg10 : "" -- globale
property msg11 : "" -- globale
property msg12 : "" -- globale
property msg13 : "" -- globale
property msg14 : "" -- globale
property msg15 : "" -- globale
property msg16 : "" -- globale
property msg17 : "" -- globale
property msg18 : "" -- globale
property msg19 : "" -- globale
property msg90 : "" -- globale
property msg91 : "" -- globale
property msg92 : "" -- globale
property msg93 : "" -- globale
property msg94 : "" -- globale
property msg99 : "" -- globale

if englishMsg is true then
	set _ to choose folder with prompt "Choose the folder storing files to 'repair'"
else
	set _ to choose folder with prompt "Choisir le dossier contenant les fichiers à corriger"
end if

open (_ as list)

-- ================

on open (sel)
	
	if msg10 = "" then my prepareMessages()
	
	tell application "Finder"
		try
			set sel to entire contents of folder sel (* utile si on a déposé un dossier
			¢ useful if we dropped a folder *)
		end try
		
		repeat with elem in sel
			set laClasse to (class of elem)
			if laClasse is not in {folder, application file} then my TraiteLeFichier(elem)
		end repeat
		
	end tell -- to Finder
end open

-- ================

on TraiteLeFichier(zefile_)
	set zefile to zefile_ as alias
	try
		set results to {msg90, "", ""} -- defaults to "unknown"
		try
			tell application "Finder"
				
				try
					if (owner privileges of zefile is not read write) then ¬
						set (owner privileges of zefile) to read write
				end try
				try
					if (locked of zefile) is true then set (locked of zefile) to false
				end try
				
				set ft to (file type of zefile) as text
				set CT to (creator type of zefile) as text
				set FTCT to ft & CT
				set deb280 to (read file (zefile as text) from 1 for 280)
				if (text 5 thru 8 of deb280) is "BOBO" then
					if (character 1 of deb280) is (ASCII character 6) then
						set marqueur to 279 -- AW6
					else
						set marqueur to 269 -- cas des versions AW4 ou AW5
					end if
					set C to ASCII number (character marqueur of deb280)
					if C = 0 then set results to {msg10 & msg91, "CWGR", "BOBO"}
					if C = 1 then set results to {msg11 & msg91, "CWWP", "BOBO"}
					if C = 2 then set results to {msg12 & msg91, "CWSS", "BOBO"}
					if C = 3 then set results to {msg13 & msg91, "CWDB", "BOBO"}
					if C = 4 then set results to {msg14 & msg91, "CWPT", "BOBO"}
					if C = 5 then set results to {msg15 & msg91, "CWPR", "BOBO"}
				else
					if "StuffIt" is in (text 1 thru 35 of deb280) then
						set results to {msg16 & msg91, "SIT5", "SIT!"}
					else
						if FTCT is in {"TEXT????", "TEXTmissing value"} then
							set results to {msg17 & msg91, "TEXT", "ttxt"}
						else
							if (deb280 starts with signRTF) and (FTCT is "missing valuemissing value") then
								set results to {msg18 & msg91, "TEXT", "ttxt"}
							else
								if (deb280 starts with signPDF) then
									if (CT is "missing value") then
										set results to {msg19 & msg91, "PDF ", "CARO"}
									else
										set results to {msg19 & " CreatorType : " & CT & msg91, "PDF ", "????"}
									end if --  (CT is ".
								end if -- (deb280 starts with signPDF).
							end if --  (deb280 starts with signRTF).
						end if --  ((FTCT = "TEXT????").
					end if -- "StuffIt" is.
				end if --  is "BOBO".
			end tell
		end try
		set {zetype, zecreator} to {results's item 2, results's item 3}
		set resultText to (results's item 1) & zetype
		
		if zecreator is "BOBO" then
			my setTypeCreator(zefile, zetype, zecreator)
			tell application "Finder"
				set leNom to (get name of zefile)
				if not (leNom ends with ".cwk") then ¬
					set name of zefile to (leNom & ".cwk")
			end tell
		else
			if zecreator is not "" then ¬
				set resultText to resultText & "/" & zecreator & " ?"
			tell application (path to frontmost application as string) to ¬
				set reponse to display dialog resultText ¬
					with icon note buttons {msg93, msg92} default button msg92
			
			set laReponse to (button returned of reponse) as text
			if (laReponse is msg92) and zetype is not "" then
				my setTypeCreator(zefile, zetype, zecreator)
				tell application (path to frontmost application as string) to ¬
					display dialog "" & zefile & return & msg94 & ¬
						zetype & "/" & zecreator & " !"
			end if -- (laReponse is msg92) and zetype.
		end if --  zecreator is "BOBO".
		
	on error MsgErr number NroErr
		if NroErr is not -128 then
			beep 2
			tell application (path to frontmost application as string) to ¬
				display dialog "" & NroErr & " : " & MsgErr with icon 0 ¬
					buttons {msg99} giving up after 20
		end if
		return
	end try
end TraiteLeFichier

-- ================

on setTypeCreator(zefile, zetype, zecreator)
	tell application "Finder"
		try
			set file type of zefile to zetype
		end try
		try
			set creator type of zefile to zecreator
		end try
	end tell
end setTypeCreator

-- ================

on prepareMessages()
	if englishMsg is true then
		set msg10 to "This is a drawing document."
		set msg11 to "This is a word processing document."
		set msg12 to "This is a spreadsheet document."
		set msg13 to "This is a database document."
		set msg14 to "This is a painting document."
		set msg15 to "This is a presentation document."
		set msg16 to "This is a stuffed document."
		set msg17 to "This is a text document."
		set msg18 to "This is a RTF text document."
		set msg19 to "This is a PDF document."
		
		set msg90 to "This document isn't recognized."
		set msg91 to return & return & "Set type/creator to "
		set msg92 to " Repair "
		set msg93 to " Cancel "
		set msg94 to "is now : "
		set msg99 to " Oops "
	else
		set msg10 to "Document vectoriel."
		set msg11 to "Document traitement de texte."
		set msg12 to "Document tableur."
		set msg13 to "Document base de données."
		set msg14 to "Document dessin bitmap."
		set msg15 to "Document présentation."
		set msg16 to "Document archive Stuffit."
		set msg17 to "Document texte."
		set msg18 to "Document texte RTF."
		set msg19 to "Document PDF."
		
		set msg90 to "Document non identifié"
		set msg91 to return & return & "Mettre type/créateur à "
		set msg92 to " Réparer "
		set msg93 to " Quitter "
		set msg94 to "est désormais : "
		set msg99 to " Vu "
	end if
end prepareMessages

--[/SCRIPT]


Yvan KOENIG (from FRANCE mardi 7 novembre 2006 20:49:20)

I’ll see what I can do. It’ll be kinda raw (no time to sift the code much at the moment), and I’ll post a link once I do.

You’re too kind. Ironically, I write all our technical documentation too. Wasn’t my original job, just fell into it. :wink:

Presto! Here’s the Code Exchange link. Hope someone finds it useful, and the tomato-throwing is kept to a low roar:

http://bbs.applescript.net/viewtopic.php?id=19095

Please be kind…this is the biggest AppleScript project I’ve ever attempted. Even a mere few months later I can see places to improve it, just no time or need to revisit it just yet.