Make droplet from existing script (extract captions)

Hi all,

I have found this great script in the scriptbuilders section from Nathan Olson to extract the IPTC-info of TIFF files. Now I am trying to make a droplet of it so I can extract the captions of many files at once and write name and caption to a text file.
However I can’t figure out why it isn’t working. Can I get some help here?

  1. No textfile is made
  2. The script doesn’t extract the caption since I changed the first line of Nathans script (see below)

Thanks for helping me out,
Kjeld

on open fileList
	set captionFileList to (((path to desktop) as string) & "Captions.txt") as file specification
	
	repeat with oneFile in fileList
		set captionFile to {}
		set fileName to the name of oneFile
		
		--From here starts Nathan Olsons script which is working perfect for one file. 
		
		--set tiffFile to (choose file with prompt "Select tif file: " of type {"TIFF"}) as string
		set tiffFile to oneFile as string
		
		set refToFile to open for access file tiffFile
		set imageFileHeader to (read refToFile from 1 to 8)
		set ifhList to {}
		
		repeat with i from 1 to 8
			set ifhList to ifhList & (ASCII number (character i of imageFileHeader))
		end repeat
		
		if (item 1 of ifhList) = 77 then
			set ifdList to reverse of (items -1 thru -4 of ifhList)
		else
			set ifdList to (items 5 thru 8 of ifhList)
		end if
		
		set ifdStart to ((my listToNumb(ifdList)) + 1)
		
		set dirEntries to (read refToFile from ifdStart to ifdStart + 1)
		if (item 1 of ifhList = 77) then
			set deList to {ASCII number (character 2 of dirEntries), ¬
				ASCII number (character 1 of dirEntries)}
		else
			set deList to {ASCII number (character 1 of dirEntries), ¬
				ASCII number (character 2 of dirEntries)}
		end if
		set numDirectories to my listToNumb(deList)
		
		set fileInfoEntry to ifdStart + 2
		set foundDirectory to false
		repeat with i from 1 to numDirectories
			set checkBytes to (read refToFile from fileInfoEntry to fileInfoEntry + 1)
			
			if (checkBytes = "ªÉ") or (checkBytes = "ɪ") then
				set foundDirectory to true
				exit repeat
			end if
			set fileInfoEntry to fileInfoEntry + 12
		end repeat
		
		if not (foundDirectory) then ¬
			display dialog ¬
				"Cutline not available." buttons "Cancel" default button 1
		
		set fiOffset to (read refToFile from (fileInfoEntry + 8) to (fileInfoEntry + 11))
		
		if (item 1 of ifhList = 77) then
			set fiList to {ASCII number (character 4 of fiOffset), ¬
				ASCII number (character 3 of fiOffset), ¬
				ASCII number (character 2 of fiOffset), ¬
				ASCII number (character 1 of fiOffset)}
		else
			set fiList to {ASCII number (character 1 of fiOffset), ¬
				ASCII number (character 2 of fiOffset), ¬
				ASCII number (character 3 of fiOffset), ¬
				ASCII number (character 4 of fiOffset)}
		end if
		set fiOffset to my listToNumb(fiList)
		
		set checkChar to (read refToFile from (fiOffset + 10) to (fiOffset + 12))
		
		
		if not (character 1 of checkChar is "x") then ¬
			display dialog ¬
				"Caption not available." buttons "Cancel" default button 1
		
		set captionCount to (text 2 thru 3 of checkChar)
		set captionList to {ASCII number (character 2 of captionCount), ¬
			ASCII number (character 1 of captionCount)}
		set captionLength to my listToNumb(captionList)
		
		set captionStart to fiOffset + 13
		set captionEnd to captionStart + captionLength - 1
		set the_Caption to (read refToFile from captionStart to captionEnd)
		close access refToFile
		
		--End of Nathan Olsons script
		
		copy (fileName & the_Caption & return) to end of captionFile
		
	end repeat
end open

try
	open for access the_file with write permission
	set eof of the_file to 0
	write (captionFile) to the_file starting at eof as list
	close access the_file
on error
	try
		close access the_file
	end try
end try


on listToNumb(theList)
	set retNumb to 0
	repeat with i from 1 to (count of theList)
		set retNumb to retNumb + (item i of theList) * (256 ^ (i - 1))
	end repeat
	return retNumb
end listToNumb

Unfortunately, the AppleScript alias object doesn’t have a name property outside of a Finder tell block. (It SHOULD, but doesn’t).


-- put this at the very top or bottom of your script document:
--
on NameOfFile( f )
	set f to f as string as alias as string -- to pathstring from alias, pathstring, fss, etc.
	tell application "Finder" to return name of item f
end NameOfFile

-- then, replace this line in your code:

	set fileName to the name of oneFile

-- with this:

	set fileName to the NameOfFile( oneFile )

Um, the code that you have at the top-level of the script, (not inside a handler), will only run if the user double-clicks the droplet. If the user drag-drops files, then only the code inside the “on open” handler will run, (and any handlers that are called from inside the “on open” handler). Move your “try” statements to just after the “end repeat” line and to just before the “end open” line.

Great Arthur. And thanks for the quick response. This works pretty good (especially after correcting some other mistakes…).
Some weird problem: at the beginning of the text file is something like this:
listutxtnIMG_0187.tif
utxtêIMG_0186.tif

etc.
Do you have any idea where it comes from and how to leave this out of the file?

That is a raw AppleScript data structure. It represents a list of Unicode strings. This is the line in your code that is doing it:


write (captionFile) to the_file starting at eof as list

You’re captionFile is a list of strings, so it gets written out in the binary-format for a list of strings. Often, this is a good thing, because it means that you can read and write complex AppleScript data structures. In this case, however, you simply want a textual report of all the strings in the list. Since you’ve already added a return character for each item in this line:


    copy (fileName & the_Caption & return) to end of captionFile

then all you have to do is coerce the whole list to a string:


    set captionFile to captionFile as string
    write (captionFile) to the_file starting at eof

You might still have problems with the text being Unicode. There is some way to tell the file system that a text document is Unicode, but I don’t know how that works.

Hey. That’s cool. Not only a solution, but an explanation as well.
Indeed there are still some problems with the unicode text. But since this code is always the same at the start and end of the files, I can deal with that. Or find out later how to convert it.

Thanks,
Kjeld

If the Unicode text only contains characters that are within the ASCII range, then you can use this to coerce from Unicode to plain MacRoman text:


-- put this at the very top or bottom of your script:
--
on AsText(str)
    --coerce Unicode or other text to styled string to plain text
    try
         return (text of ((str as string) as record)) as string
    end try
    return str -- if plain text to begin with
end AsText

    -- then change your previous code to this:
    --
    set captionFile to captionFile as string
    set captionFile to AsText( captionFile )
    write (captionFile) to the_file starting at eof

thanks man. I got it working.