Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Click here to return to the 'Bulk convert text files to PDF' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Bulk convert text files to PDF
Authored by: tedw on Jun 07, '11 07:38:19PM
I'm not sure what your objection to text items is. for instance, you could replace your last two subroutines with the following code:
on changeExtension(myText, myExt)
	set {oldTID, my text item delimiters} to {my text item delimiters, "."}
	if last text item of myText is in {"txt", "rtf", "doc", "docx", "htm", "html", "js", "h", "c"} then
		set output to (text items 1 thru -2 of myText) & myExt as text
		set output to {myText, myExt} as text
	end if
	set my text item delimiters to oldTID
	return output
end changeExtension
which seems clearer and simpler to me than the whole 'length-offset' system you've used, and is likely faster (text items are very quick). You might need to tweak the list of valid extensions out if you want to use this on other file types, of course.

[ Reply to This | # ]
Bulk convert text files to PDF
Authored by: wallybear on Jun 08, '11 06:46:05AM
Nice solution.
Oh, I have nothing against your code, only I don't like tampering with the text item delimiters (a strange form of idiosyncrasy, I hope that a cure exists). For text manipulation (and other stuff) I often make direct calls to Cocoa NSString functions.

My first rewrite of your script was much more short (7 lines for the main procedure and the 3 lines of the lastPos function), but then I noticed the possibilty of errors so I added a little more control on the script, that's why I had to add the changeExtension function and more lines in the main script.

Note that your changeExtension suggestion does not change the extension in every situation: if the extension is in the list you supply it will be changed, otherwise the new extension will be appended without removing the previous one.
That solves cases with a file with no extension (and the consequential risk of a folder in the path containing a dot), but you have to list all possible accepted extensions for the script, and the list is really long: the convert tool can convert to PDF a lot of file types (e.g. TIFF, GIF, PSD and more graphic formats).

BTW for text manipulation I often make direct calls to Cocoa functions
Edited on Jun 08, '11 06:48:19AM by wallybear

[ Reply to This | # ]
Bulk convert text files to PDF
Authored by: tedw on Jun 08, '11 07:43:01AM
Sorry, there's no cure for that, and I'm pretty sure it's fatal. :-)

'shortness' is only one factor here clarity is also important. You identify the extension using (essentially):

(length of myText) - (offset of "." in ((reverse of text items of myText) as string))
I use:
set my text item delimiters "."
last text item of myText
If you like using cocoa commands for text manipulation, then you're stuck in XCode, though personally for any complex text manips I use the Satimage osax (which has all sorts of text goodies wrapped up in it). text items take some getting used to, but they really simplify some kinds of text operations.

with respect to your other point, you're right, but that's somewhat out of the scope of this script. all this script (unmodified) can do with images is create a pdf for each one fed to it, which isn't all that useful - if you want to create multi-image pdfs then you'd want a different script (and even then, you'd probably have more luck making pdfs straight from preview). Can't do everything...

[ Reply to This | # ]

Bulk convert text files to PDF
Authored by: wallybear on Jun 08, '11 10:16:36AM

Too bad for the cure... :(

You're right, the text delimiters method is clearer; regarding Satimage osaxes they are really interesting, but long ago I promised myself to keep clear from osaxes (I got burned when some osaxes I used in my software stopped working with the next OS X release and found to be no more mantained); what's more, I should force my users to install them.

Ok for the (un)usefulness of converting images, but it was only an example; convert can process lots of different file kinds, and I don't know "a priori" which one I have to put in that list. Why should I limit a functionality given for free?

Regarding the deletion of the temp file when converting from doc/docx, I just found that the "-D" option of convert deletes the input file when finished, so it could be used as an alternative method.

[ Reply to This | # ]