AppleScript help

Discussion in 'Mac Programming' started by Mechcozmo, Sep 26, 2004.

  1. macrumors 603

    Mechcozmo

    Joined:
    Jul 17, 2004
    #1
    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!
     
  2. Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #2
    I got this from another site. Perhaps it will do what you want.

    Code:
    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
     
  3. Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #3
    I wrote that :eek: :D

    It was originally in this 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. :)
     
  4. thread starter macrumors 603

    Mechcozmo

    Joined:
    Jul 17, 2004
    #4
    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:
     
  5. Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #5
    Thanks, HexMonkey. I didn't remember so I'm sorry I didn't give you credit.
     
  6. Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #6
    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.

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

    Joined:
    Jun 23, 2006
    #7
    possible simple solution

    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
     

Share This Page