PDA

View Full Version : AppleScript help




Mechcozmo
Sep 26, 2004, 05:25 PM
Just want to make a very basic AppleScript that, once pointed at a folder will rename them all to: "8993-myname-1", the '1' would be incremented for each picture. I have Visual Basic.NET programming experience, as well as some C++, but AppleScript always seems to elude me...

Thanks!



Doctor Q
Sep 26, 2004, 05:48 PM
I got this from another site. Perhaps it will do what you want.

property nameString : ""
property extension : ""

tell application "Finder" to set theFiles to selection

if number of items in theFiles is greater than 0 then
display dialog "Please enter the new name for the files:" default answer nameString
set nameString to text returned of the result
display dialog "Enter a file extension, or leave blank for none:" default answer extension
set extension to text returned of the result
if the nameString is not "" then
repeat with i from 1 to number of items in theFiles
set thisItem to item i of theFiles as alias
set thisInfo to info for thisItem
set currentName to name of thisInfo
if extension is "" then
set extensionString to ""
else
set extensionString to "." & extension
end if
if i is 1 then
set newName to nameString & extensionString
else
set newName to nameString & " " & (i as string) & extensionString
end if
my setItemName(thisItem, newName)
end repeat
end if
else
display dialog "No items are selected" buttons "OK" default button 1
end if

on setItemName(thisItem, newName)
tell application "Finder"
set parentFolder to (container of thisItem) as text
if not (exists item (parentFolder & newName)) then
try
set name of thisItem to newName
on error errorMessage number errorNumber
tell me to display dialog ("An error occured when renaming to" & newName & ". The file will not be renamed.") buttons "OK" default button 1
return 0
end try
else --the name already exists
tell me to display dialog ("A file named" & newName & " already exists. The file will not be renamed.") buttons "OK" default button 1
return 0
end if
end tell
end setItemName

HexMonkey
Sep 26, 2004, 06:13 PM
I wrote that :eek: :D

It was originally in this (http://forums.macrumors.com/showthread.php?t=83949) thread, which has some more information about the script and some other possible solutions. If you have any questions about the script or want some changes, just ask. :)

Mechcozmo
Sep 26, 2004, 06:26 PM
Very neat. The program, BTW, is for Yearbook. The two photographers (me and someone else) need to rename the pictures according to some style that the printer needs.

The other photographer has a 15" PowerBook (Rev. B or C, dunno)

The only thing that I wonder about, is that whole order thing that is mentioned in the other post. But, I guess if I were to tell that window to be displayed acording to Date Created, everything would be fine and dandy? :confused:

Thank you so much, in any case.

EDIT: Tested it out, the only problem I forsee is with the name extensions, but if I tell it .jpg it should work...I still like the OS 9 way of doing things, with the Creator codes. But it does work, and work well. :) Now I don't have to rename 100 pictures a week by hand! :) :cool:

Doctor Q
Sep 26, 2004, 06:47 PM
I wrote thatThanks, HexMonkey. I didn't remember so I'm sorry I didn't give you credit.

HexMonkey
Sep 26, 2004, 07:12 PM
The only thing that I wonder about, is that whole order thing that is mentioned in the other post. But, I guess if I were to tell that window to be displayed acording to Date Created, everything would be fine and dandy? :confused:

The problem is that there is no defined order that Finder uses to communicate its selection. Sometimes it's seemingly random, sometimes it's ordered. Changing the view in Finder to date created will not necessarily help. The only workaround I know of is to look at the contents of a folder rather than a list of files, which then sorts them by name. If that would be useful, tell me and I'll see if I can make the changes.

Thanks, HexMonkey. I didn't remember so I'm sorry I didn't give you credit.

That's alright, as long as the code is helpful. :)

zippyz
Jun 23, 2006, 03:52 AM
If I understand your problem, then the script I have just written ought to work for you. Here it is:

set folderpath to (choose folder with prompt "Which folder would you like to rename the contents of?")
tell application "Finder"
set filelist to files in folderpath
set n to 0
repeat with afile in filelist
set newfilename to "8993-myname-" & n + 1
set name of afile to newfilename
set n to n + 1
end repeat
end tell

If this script is not quite what you want, please contact me either via the forum or email at cj.lusty@ukf.net.
Good luck!

p.s. this script is based on, but not copied from, a script in the book "Beginning mac os x programming" by Stephen Kochan. I highly recomend it to anyone wanting to gain a thorough grounding in the basics of applescript