newbie: create a link for every file in a folder


i wanted to write a script that creates a

link for every file in a folder

i got to the part where i can read contents of a folder and put this into a file, but only with bash
and i cant seem to include a repeat loop into the middle of a bash syntax

set theFile to "/Users/xax/Desktop/snf/lol.txt"
set line1 to "<a href=\""
set line2 to "\" target=_blank>"
set line3 to "<img src=\""
set line4 to "\" border=0></a>"

do shell script "echo " & quoted form of line1 & "blah" & quoted form of line2 & quoted form of line3 & "blobb" & quoted form of line4 & " > " & theFile

i only got that far. i would need every file in a folder instead of “blah”, so it results in a file which shows:

i hope i could make myself more or less understandable…

every help is welcome, thanks upfront already, i already learned a lot from this board

ps: my real name is XiaoSu Han, since i got deleted once already i think

Try something like this:

choose folder with prompt "Create links for files in this folder:"

set nameList to list folder result without invisibles

set newFileContent to {}
repeat with thisName in nameList
	set newFileContent's end to "<a href\"" & thisName & "\" target=\"_blank\"><img src=\"tn_" & thisName & "\" border=\"0\"></a>"
end repeat

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {ASCII character 10}
set newFileContent to newFileContent as Unicode text
set AppleScript's text item delimiters to ASTID

choose file name

	set fileRef to open for access (result) with write permission
	write newFileContent to fileRef starting at 0
	close access fileRef
on error errMsg number errNum
		close access fileRef
	end try
	display dialog "Error " & errNum & ":" & return & return & errMsg buttons "Cancel" default button 1 with icon caution
end try

*Looks like Bruce beat me to it. Anyway…

Hi There.

Maybe assemble your string outside of the shell script?

You can hardcode the folder path and use “ls”, add a check to make sure they’re all jpg’s etc., but here it is using the finder:

set theFile to "/Users/xax/Desktop/snf/lol.txt"
set line1 to "<a href=\""
set line2 to "\" target=_blank>"
set line3 to "<img src=\""
set line4 to "\" border=0></a> "

tell application "Finder"
	set theFolderList to list folder (choose folder) without invisibles
end tell

set bigString to ""
repeat with aFileName in theFolderList
		set theText to line1 & aFileName & line2 & line3 & "tn_" & aFileName & line4
	set bigString to bigString & theText
end repeat

do shell script "echo " & quoted form of bigString & " > " & theFile


List folder is part of Standard Additions, not Finder.

These are equivalent statements:

tell application "Finder" to set f to name of items of (choose folder)
set g to list folder (choose folder) without invisibles
{f, g}

thanks a lot guys!
i’ll report back with the results when i get home tonight!



We could go a step further with this, and have the script also actually create the resized thumbnail images for you.

Just point it to a group of regular images, and have it resize the photos to a specific width or height.

If anyone feels like tackling this, it’d be cool. I think I’ll try to work on it. (I’m just not very good). :slight_smile:

I’ve been working on improving a (currently private) script that does this. FWIW, I found it easier to use ImageMagick (a command-line tool).

See also (ImageMagick):
Prepacked Installer
Compiling Tips
Guide To Building ImageMagick For Mac OS X (PDF)

Is it not possible to use Image Events or something? That’s the route I was going to try to take. It sure would be cool to not require 3rd party app. But if I must, I must.

BIg fan of ImageMagick, but I havent been thrilled with the quality of the output in certain situations. Then again, I haven’t fully explored all of the format options.

I use a AS to call Photoshop Actions. I find it easy to set up an action where I’m able to tweak quality (dimensions, post resize sharpening, jpeg quality, etc) on a batch by batch basis if necessary.

It’s not nearly as fast as ImageMagick, but I’m only doing a few hundred images at a time, not thousands.

As a side note, I’ve never gotten consistent results from Image Events. I dunno why, but something always seems to go wrong when I try to incorporate it into a workflow.


Maybe Bruce will make his private script public in the near future :wink:

checked it out now, thanks a lot again, works like a charm!