Renaming files error

Hello,

I’m new to Applescript and new to this forum at the same time. I wanted to use applescript for one specific task: batch renaming of image files. I found a script on the first few pages of Matt Neuburg’s “AppleScript - The Definitive Guide”, but was disappointed to find that his example strips all the file extensions! What use is that? So I tried to change his code to attach a file extension, but I get an error. Here is the script:

on open folderlist
	repeat with aFolder in folderlist
		if kind of (info for aFolder) is "Folder" then
			renameStuffIn(aFolder)
		end if
	end repeat
end open
on renameStuffIn(theFolder)
	set ix to 0
	tell application "Finder"
		set allItems to (get every item of theFolder)
		repeat with thisItem in allItems
			set ix to ix + 1
			set newName to ix & ".jpg"
			set name of thisItem to newName
		end repeat
	end tell
end renameStuffIn

And here is the error: Can’t make some data into the expected type.

This leaves me scratching my head, since I found countless examples online (one of them here in the forums), where file extensions seem to be attached just this way.

So what am I doing wrong?

I’d be really grateful for your help.


OS 10.4.8
Scripteditor 2.1.1 (81)
Applescript 1.10.7

Hi Manfred,

the crucial line is:

set newName to ix & ".jpg"

ix is an integer, but a string is needed.
This helps:

set newName to (ix as string) & ".jpg"

Thank you, Stefan. It works!

PS: (almost) the same script worked in Matt’s book,
because there the filename comes first and AppleScript coerces a following integer automatically to a string.
In your script the integer comes first and the attempt to coerce the following string to a integer fails

And if you want it to work for extensions other than “jpg”, this does it:


on renameStuffIn(theFolder)
	set ix to 0
	tell application "Finder"
		set allItems to items of theFolder
		repeat with thisItem in allItems
			set ix to ix + 1
			set Ext to name extension of thisItem
			set newName to ix as text
			set name of contents of thisItem to newName & "." & Ext
		end repeat
	end tell
end renameStuffIn

renameStuffIn(choose folder)

Thanks for explaining, Stefan. I see (and hope I’ll remember).

Adam, why

set name of contents of thisItem to newName & "." & Ext

and not

set name of thisItem to newName & "." & Ext

What does “contents of” do?

Einen Gruß in die schwäbische Heimat (ich komme aus Stuttgart) :slight_smile:

Adam isn’t currently online, so I’ll try to answer the question

the index variable of the repeat with x in theList form refers not to the item itself,
is stands for "item x of {theList}. To get access to the properties of the index variable,
you must use “contents of”

Grüßle z’rück :slight_smile:


I see, but why does my example (with your corretions) work without “contents of”, then?

this is one of the secrets of AppleScript:

Sometimes it works anyway

No joke, in some cases one expression works in some application but doesn’t in another.
Always a certain percentage rate in AppleScript is trial and error

Thank you. I’m just finishing the installation of a new kitchen floor, so look in only occasionally.

It’s hard to know without experiments whether “item x of {theList}” will coerce to the item itself or not.

These work without “contents of”:


set theList to {"alpha", "beta", "gamma", "delta"}
repeat with L in theList
	if L contains "g" then display dialog "Found " & L
end repeat

tell application "Finder"
	set tItems to items of desktop as alias list
	set S to 0
	repeat with L in tItems
		if name extension of L is "scpt" then
			set S to S + 1
		end if
	end repeat
	display dialog "Found " & S & " scripts on your desktop"
end tell

This doesn’t:


set theList to {"alpha", "beta", "gamma", "delta"}
repeat with I in theList
	if I contains "g" then set I to "George"
end repeat
theList --> {"alpha", "beta", "gamma", "delta"}

But:


set theList to {"alpha", "beta", "gamma", "delta"}
repeat with I in theList
	if I contains "g" then set contents of I to "George"
end repeat
theList --> {"alpha", "beta", "George", "delta"}

Right, setting or changing a value of an index variable works only with “contents of”
getting the value works without, too