Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 19, 2012, 03:13 PM   #1
hadirajan
macrumors newbie
 
Join Date: Feb 2012
AppleScript renaming files with unique name

Hello everyone,

Hope you go easy on me since I'm quite new to the Mac world, used PCs for the last 30 years of my life but has now decided to leave the dark side

I'm having some trouble solving something that I'm used to doing on PCs on the Mac, and it's about renamning files.
I've read a lot of articles and forum posts and also checked a lot of youtube clips on AppleScript, Automater and Terminal but haven't found the answer to my question.

What I want it pretty simple.
I have a bunch of files that I would like to add an predefined but unique text to, see example below.
Old file name New file name
File1.xxx File1-uniquetext1.xxx
File2.xxx File2-uniquetext2.xxx
File3.xxx File3-uniquetext3.xxx
File4.xxx File4-uniquetext4.xxx
File5.xxx File5-uniquetext5.xxx
File6.xxx File6-uniquetext6.xxx

I used to do this really simple on windows using a .bat file.
Got the original name from the folder with the files.
Got the unique texts( in this case TV-show titles) from the internet.
Then did some excel magic and put together an .bat file looking like this.
rename File1.xxx File1-uniquetext1.xxx
rename File2.xxx File2-uniquetext2.xxx
rename File3.xxx File3-uniquetext3.xxx
rename File4.xxx File4-uniquetext4.xxx
rename File5.xxx File5-uniquetext5.xxx
rename File6.xxx File6-uniquetext6.xxx


Anyone able to help me with this using AppleScript?
Would really save my day and help me with not having to return to Windows for a simple task like this.
I appreciate all help!

Thanks in advance!!
hadirajan is offline   0 Reply With Quote
Old Feb 19, 2012, 03:37 PM   #2
dylanryan
macrumors newbie
 
Join Date: Aug 2011
You'd be better off using a shell script for this.

Basically, do the exact same thing you would for a bat file, only instead of "rename" use "mv" (so you'd have lines like "mv File1.xxx File1-uniquetext1.xxx" .

Just add "#! /bin/bash" as the first line of the file, and save it with a .sh extension. Then, in terminal, perform "chmod u+x yourFile.sh" (replace the file name as needed, do not include quotes), and then "./yourFile.sh" to run it. You can edit the script later with different sets of files and you won't need to re-run the chmod line.

Edit: The above assumes you put the shell script in your home folder. If it is somewhere else, use the full path to the file in both cases.

Note: If the files to be renamed are not in your home folder, either put the full paths in the shell script (so "mv /full/path/to/File1.xxx /full/path/to/File1-uniquetext1.xxx" ), or put the shell script into the same folder as all the files, and before you do the chmod above, type "cd " (not the trailing space, and do NOT hit enter), then drag the folder containing the script into Terminal. Then press enter. That will put you into the right directory, so you can then chmod it to make it executable and then run it with the ./yourFile.sh line.

Last edited by dylanryan; Feb 19, 2012 at 03:48 PM.
dylanryan is offline   0 Reply With Quote
Old Feb 19, 2012, 04:40 PM   #3
hadirajan
Thread Starter
macrumors newbie
 
Join Date: Feb 2012
Quote:
Originally Posted by dylanryan View Post
You'd be better off using a shell script for this.

Basically, do the exact same thing you would for a bat file, only instead of "rename" use "mv" (so you'd have lines like "mv File1.xxx File1-uniquetext1.xxx" .

Just add "#! /bin/bash" as the first line of the file, and save it with a .sh extension. Then, in terminal, perform "chmod u+x yourFile.sh" (replace the file name as needed, do not include quotes), and then "./yourFile.sh" to run it. You can edit the script later with different sets of files and you won't need to re-run the chmod line.

Edit: The above assumes you put the shell script in your home folder. If it is somewhere else, use the full path to the file in both cases.

Note: If the files to be renamed are not in your home folder, either put the full paths in the shell script (so "mv /full/path/to/File1.xxx /full/path/to/File1-uniquetext1.xxx" ), or put the shell script into the same folder as all the files, and before you do the chmod above, type "cd " (not the trailing space, and do NOT hit enter), then drag the folder containing the script into Terminal. Then press enter. That will put you into the right directory, so you can then chmod it to make it executable and then run it with the ./yourFile.sh line.

Thanks for the quick help dylanryan!! Will try this!
Thanks for good tips on the Terminal as well, really like the cd+"drag and drop folder"-part
Can you explain the nessesity of the "#! /bin/bash" in the shell script file?
And also what the "chmod u+x" stands for?
Sorry, just trying to learn :-)


After a few tries of my own I got it working by using this AppleScript:

tell application "Finder"
set the name of file "File1.xxx" to "File1-uniquetext1.xxx"
set the name of file "File2.xxx" to "File2-uniquetext2.xxx"
set the name of file "File3.xxx" to "File3-uniquetext3.xxx"
set the name of file "File4.xxx" to "File4-uniquetext4.xxx"
set the name of file "File5.xxx" to "File5-uniquetext5.xxx"
set the name of file "File6.xxx" to "File6-uniquetext6.xxx"
end tell

However I had to put the files on the desktop for this to work for some reason, any guesses?
Tried like a fool to get it to work if I had the files in a different folder but was unsuccessful, even if the folder was Desktop/Files/...
Can someone explain how to use this script but pointing to a specific folder?
I'm trying to learn AppleScript at the same time so some explanation would be really appreciated!

Thanks!
hadirajan is offline   0 Reply With Quote
Old Feb 19, 2012, 04:50 PM   #4
dylanryan
macrumors newbie
 
Join Date: Aug 2011
#! /bin/bash tells the system what shell to run the script with. In this case, that is a bash script, so we tell it to use the bash executable located in /bin. See also http://en.wikipedia.org/wiki/Shebang_(Unix)

chmod stands for CHange MODe. u+x in this case tells it to set (+) the eXecutable bit for your User. TO learn more on unix commands, in terminal, type "man theCommand". For example, "man chmod", "man mv", "man bash", "man man", etc. (man is short for MANual).

I rarely use applescript, but my guess is the problem is that the files are not being specified by their path. My first guess would be to prepend the full path to all files. I.e. if it works on the desktop, but your files are in a folder named Files on the desktop, then change every file to "Files/theOldName". However, I think Applescript uses different path separators, so you may need to use a : instead of a / (again, I'm not an applescript expert by any means). Basically, since your computer could have 50 files named "someName", you have to specify it exactly by giving the full path, either form the root of the hard drive, or from the "current working directory" (and I have no idea what the working directory for applescripts are, but it sounds like it is the desktop, or perhaps wherever the applescript was saved).
dylanryan is offline   0 Reply With Quote
Old Feb 19, 2012, 04:59 PM   #5
hadirajan
Thread Starter
macrumors newbie
 
Join Date: Feb 2012
Quote:
Originally Posted by dylanryan View Post
#! /bin/bash tells the system what shell to run the script with. In this case, that is a bash script, so we tell it to use the bash executable located in /bin. See also http://en.wikipedia.org/wiki/Shebang_(Unix)

chmod stands for CHange MODe. u+x in this case tells it to set (+) the eXecutable bit for your User. TO learn more on unix commands, in terminal, type "man theCommand". For example, "man chmod", "man mv", "man bash", "man man", etc. (man is short for MANual).

I rarely use applescript, but my guess is the problem is that the files are not being specified by their path. My first guess would be to prepend the full path to all files. I.e. if it works on the desktop, but your files are in a folder named Files on the desktop, then change every file to "Files/theOldName". However, I think Applescript uses different path separators, so you may need to use a : instead of a / (again, I'm not an applescript expert by any means). Basically, since your computer could have 50 files named "someName", you have to specify it exactly by giving the full path, either form the root of the hard drive, or from the "current working directory" (and I have no idea what the working directory for applescripts are, but it sounds like it is the desktop, or perhaps wherever the applescript was saved).
Thanks again!
Sounds like the shell script is more like my alley since it reminds me of dos commands. Thanks for the clarification also, that really helps.

When it comes to the AppleScript I really fell short on the folder specification. Felt like I tried all kinds of ways to specify the location of the files but nothing seemed to work.
So if there's anyone out there who could clarify this I would be a very very happy camper :-)
hadirajan is offline   0 Reply With Quote
Old Feb 19, 2012, 05:08 PM   #6
dylanryan
macrumors newbie
 
Join Date: Aug 2011
I just made a folder named "Test" on my desktop, and put a file named "Testing.txt" inside it.

This applescript worked:

Code:
tell application "Finder"
	set the name of file "Test:Testing.txt" to "Testing2.txt"
end tell
So, it looks like applescript uses :'s to separate paths, and if you are using set the name of file, it doesn't need a path on the final name. And it looks like the files are found relative to the desktop.
dylanryan is offline   0 Reply With Quote
Old Feb 19, 2012, 06:20 PM   #7
hadirajan
Thread Starter
macrumors newbie
 
Join Date: Feb 2012
Quote:
Originally Posted by dylanryan View Post
I just made a folder named "Test" on my desktop, and put a file named "Testing.txt" inside it.

This applescript worked:

Code:
tell application "Finder"
	set the name of file "Test:Testing.txt" to "Testing2.txt"
end tell
So, it looks like applescript uses :'s to separate paths, and if you are using set the name of file, it doesn't need a path on the final name. And it looks like the files are found relative to the desktop.
Very interesting, now I only need to figure out how to set the path for my NAS and I'm good to go :-)
Thanks for testing and taking your time with this!
hadirajan is offline   0 Reply With Quote
Old Feb 21, 2012, 04:30 PM   #8
hadirajan
Thread Starter
macrumors newbie
 
Join Date: Feb 2012
Quote:
Originally Posted by hadirajan View Post
Very interesting, now I only need to figure out how to set the path for my NAS and I'm good to go :-)
Thanks for testing and taking your time with this!
Seems like that wasn't such a hard part that either, got it working now.
Thanks so much for the help dylanryan!
I always tried to use the location both for the original file and in the part where the new name was going to be and that messed it up. Now everything is working perfectly.
hadirajan is offline   0 Reply With Quote
Old Feb 21, 2012, 05:28 PM   #9
dylanryan
macrumors newbie
 
Join Date: Aug 2011
No problem. My first try used the full location for the destination too. I saw that that didn't produce errors, but also didn't move anything, so my next try was to just remove the full destination.

In hindsight, it does make some sense since we are asking it to rename the file. You have to tell it what file you want to rename (full path), and then the new name (no full path). My mistake was thinking about it in terms of moving the file and in the process renaming (in that case, a full path for the destination would have made sense).

Anyway, glad you got it working!
dylanryan is offline   0 Reply With Quote
Old Feb 22, 2012, 02:49 PM   #10
Mark FX
macrumors member
 
Join Date: Nov 2011
In Applescript you have to construct a path to the desired folder.
for example this code snippet is your Documents folder.

Code:
tell application "Finder"
	set folderPath to (path to documents folder) as alias
end tell
Notice that a folder or file path has to be an alias type.

But if you want to build a folder path that is not one of the standard folders,
then you would construct it like this.

Code:
tell application "Finder"
	set folderPath to (path to desktop folder) & "Special Files" as text
	set folderPath to folderPath as alias
end tell
This would point to a folder called "Special Files" on your desktop, also notice
that I then converted it to an alias type, so that Finder can understand it.
But please be aware if you try to make an alias type that does not exist, then
you will get an error, so you would try to trap the error like this.

Code:
tell application "Finder"
	set folderPath to (path to desktop folder) & "Special Files" as text
        try
	        set folderPath to folderPath as alias
        on error
                return "An error occured" as text
        end try
end tell
So as to your original question, if you have a folder called "My Files" on your
desktop, with a file in it called "Test.txt" you would rename it like this.

Code:
tell application "Finder"
	set folderPath to (path to desktop folder) & "My Files" as text
	try
		set folderPath to folderPath as alias
		set the name of file "Test.txt" in folder folderPath to "Test2.txt"
	on error errorMsg number errorNum
		display alert errorMsg & space & errorNum message 
			"An error occured trying to find the file" & return & 
			folderPath & "Test.txt" & return & 
			"Check the entered file path exists." as warning
		return
	end try
end tell
I hope this helps you understand Applescript paths a bit better, but be aware
there is a lot more you can learn by reading some Applescript tutorials, or
reading a book on the subject.

Also you should read the different applications scripting definition files, to learn
more about what different Mac applications can do for you with Applescript.

Regards Mark
Mark FX is offline   0 Reply With Quote
Old Feb 22, 2012, 04:12 PM   #11
hadirajan
Thread Starter
macrumors newbie
 
Join Date: Feb 2012
Quote:
Originally Posted by Mark FX View Post
In Applescript you have to construct a path to the desired folder.
for example this code snippet is your Documents folder.

Code:
tell application "Finder"
	set folderPath to (path to documents folder) as alias
end tell
Notice that a folder or file path has to be an alias type.

But if you want to build a folder path that is not one of the standard folders,
then you would construct it like this.

Code:
tell application "Finder"
	set folderPath to (path to desktop folder) & "Special Files" as text
	set folderPath to folderPath as alias
end tell
This would point to a folder called "Special Files" on your desktop, also notice
that I then converted it to an alias type, so that Finder can understand it.
But please be aware if you try to make an alias type that does not exist, then
you will get an error, so you would try to trap the error like this.

Code:
tell application "Finder"
	set folderPath to (path to desktop folder) & "Special Files" as text
        try
	        set folderPath to folderPath as alias
        on error
                return "An error occured" as text
        end try
end tell
So as to your original question, if you have a folder called "My Files" on your
desktop, with a file in it called "Test.txt" you would rename it like this.

Code:
tell application "Finder"
	set folderPath to (path to desktop folder) & "My Files" as text
	try
		set folderPath to folderPath as alias
		set the name of file "Test.txt" in folder folderPath to "Test2.txt"
	on error errorMsg number errorNum
		display alert errorMsg & space & errorNum message 
			"An error occured trying to find the file" & return & 
			folderPath & "Test.txt" & return & 
			"Check the entered file path exists." as warning
		return
	end try
end tell
I hope this helps you understand Applescript paths a bit better, but be aware
there is a lot more you can learn by reading some Applescript tutorials, or
reading a book on the subject.

Also you should read the different applications scripting definition files, to learn
more about what different Mac applications can do for you with Applescript.

Regards Mark
Hey Mark!

Thanks a million! That really helps me understand some of the scripts I've been finding online, great help.
I'm thinking about getting a good book to read on AppleScripting, is there anyone you could suggest for some one new to this like me?
hadirajan is offline   0 Reply With Quote
Old Mar 5, 2012, 05:25 AM   #12
MacGrunt
macrumors newbie
 
Join Date: Jul 2011
G'day

There's HEAPS of stuff online including of course Apple's own documentation and tutorials and samples.

As a beginner I found Ben Waldie's stuff at mactech.com really useful. And Macscripter.com is a great resource too.

So far I've only bought one book, but I DO refer to it often Applescript 1-2-3 by Sal Soghoian and Bill Cheeseman. It's really good at explaining all that stuff about paths and folders and aliases and POSIX files, etc, etc.

Learning Applescript can be frustrating, but it's well worth perservering.
Have fun with your journey.

m.
MacGrunt is offline   0 Reply With Quote
Old Jun 3, 2012, 12:05 PM   #13
polarOpposite
macrumors newbie
 
Join Date: Jun 2012
So, I want to do the same thing (rename lots of files with unique text) but is there a way, either in the shell or applescript, for it to import the unique text as a string from a text or csv file? I know this is possible in powershell, but I havent seen anything like this with applescript.
polarOpposite is offline   0 Reply With Quote
Old Jun 3, 2012, 02:49 PM   #14
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
A Google search on "applescript rename files from text file" will get you started. You can read a txt file with the following code :

Code:
set file_ref to (choose file with prompt "Select a file to read:")
set files_list to paragraphs of (read file_ref)
-- Result : {"First filename.jpg", "Second filename.jpg", "Third filename.jpg", ""}
--the following lines are to remove the last item in the list eg ""
set files_list_item_count to count files_list
set files_list to (items 1 thru (files_list_item_count - 1)) of files_list
-- Result : {"First filename.jpg", "Second filename.jpg", "Third filename.jpg"}
Attached Files
File Type: txt filenames.txt (58 Bytes, 77 views)
kryten2 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
How to batch rename files with a random name?? amiskwia OS X 30 Jan 8, 2013 07:33 AM
Applescript: renaming files in a system folder without having to enter admin password Compton Mac Programming 7 Sep 27, 2011 04:43 AM
Applescript to copy highlighted folder name to iCal ToDo List inaka Mac Programming 0 Sep 15, 2011 07:38 PM
Applescript rename files wesg Mac Programming 1 Nov 2, 2008 08:46 AM
Renaming files with Automator Jrobinsonphoto Mac Programming 2 Jan 28, 2008 12:10 PM


All times are GMT -5. The time now is 09:26 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC