Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

hadirajan

macrumors newbie
Original poster
Feb 19, 2012
7
0
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!!
 

dylanryan

macrumors member
Aug 21, 2011
57
2
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:

hadirajan

macrumors newbie
Original poster
Feb 19, 2012
7
0
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 :D
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!
 

dylanryan

macrumors member
Aug 21, 2011
57
2
#! /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).
 

hadirajan

macrumors newbie
Original poster
Feb 19, 2012
7
0
#! /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 :)
 

dylanryan

macrumors member
Aug 21, 2011
57
2
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.
 

hadirajan

macrumors newbie
Original poster
Feb 19, 2012
7
0
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

macrumors newbie
Original poster
Feb 19, 2012
7
0
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.
 

dylanryan

macrumors member
Aug 21, 2011
57
2
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!
 

Mark FX

macrumors regular
Nov 18, 2011
159
17
West Sussex, UK
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
 

hadirajan

macrumors newbie
Original poster
Feb 19, 2012
7
0
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?
 

MacGrunt

macrumors newbie
Jul 25, 2011
7
0
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.
 

polarOpposite

macrumors newbie
Jun 3, 2012
1
0
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.
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
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"}
 

Attachments

  • filenames.txt
    58 bytes · Views: 297
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.