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

i.shaun

macrumors 6502a
Original poster
May 1, 2008
784
0
Canada
sorry, I posted this already in mac basics, but I think this may be a better spot for it.

I'm trying to write an apple script to attach to an image folder that will:

-add a number to the beginning of a file in sequence as I drop them into the folder.

example:

images are named:
image1.jpg
image2.jpg
-----------------
I want to drop them into the folder, and have it automatically add the prefix:
0001_image1.jpg
0002_image2.jpg



Now keep in mind I have no idea what I'm doing here. I've spent the last few hours trying to "reverse engineer" pre-made apple scripts, learning what I can about the commands and such. This is very confusing, but this is what I got so far:
-----------------

tell application "Finder"
set the_counter to 1
set new_name of added_items in file_list in folder this_folder to (the_counter & "- " & filename & ext)
end tell
---------------------------
Any help would be appreciated.


it now says "the variable "filename" is not defined."


edit: I updated the script as I have it so far. Still not working. Sometimes I wish I knew what I was doing.
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
I have written an application in Applescript when I was 15-16 years old... It was downloaded thousands of times before it was abandoned by me in favor of C++ development. Maybe you should check it out...

This is Finder utility and it is located in my old site...
 

i.shaun

macrumors 6502a
Original poster
May 1, 2008
784
0
Canada
looks like a handy utility, but it doesn't quite do what I was trying to do.

I wanted a folder action to automatically add a number to files as they arrive. I didn't think it would be so difficult as it can convert and do other things to items as they arrive.

I was going to drop photos in 1 at a time, and have them auto-numbered. This script you wrote seems to be for adding numbers to files already in a folder together.
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
looks like a handy utility, but it doesn't quite do what I was trying to do.

I wanted a folder action to automatically add a number to files as they arrive. I didn't think it would be so difficult as it can convert and do other things to items as they arrive.

I was going to drop photos in 1 at a time, and have them auto-numbered. This script you wrote seems to be for adding numbers to files already in a folder together.

Yes, the purpose of the application was to give you an example. Well, I have given up Applescript a long a ago, but I can suggest something that might help you.

In your script, add a property that will hold the current number of the files that are added in your folder. Each time it runs, the script will increase that number. Properties in Scripts are saved in your preferences folder in your user folder (inside your Library).

Another thing you can do is to check the already existing files inside a folder when adding a new item. Read each file's name until the first whitespace, then try casting that into a number. Then, you will have the numbers of the files that exist in the folder. By finding the greater one of them, you can add a number to the file that already does in there.

I state again that I stopped programming in Applescript long ago, but that is the general idea.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I think Soulstorm has covered this, but the simplest thing in my mind is to just get a count of the files in the folder, add one, and use that to name the file added.

-Lee
 

i.shaun

macrumors 6502a
Original poster
May 1, 2008
784
0
Canada
thanks for the help. I did look at your script for an idea, but I can't make sense of it (I really don't know anything about apple script, or any programming)

I was just trying to see if I could take parts of other scripts and have it do what I wanted. All scripts I see so far are very confusing.


I guess I'll abandon this as I seem way over my head here.
 

Cromulent

macrumors 604
Oct 2, 2006
6,817
1,102
The Land of Hope and Glory
I guess I'll abandon this as I seem way over my head here.

Don't give up :). This a perfect opportunity to learn something new. I'm sure there are loads of people who will help you out on this forum if you run into trouble.

You'll be happy to know that Applescript is probably the easiest scripting language to learn in existence (that I have seen anyway). Sure, at first it looks a bit confusing but if you actually read the script rather than trying to understand it, the script makes sense. It is basically just like reading a shopping list in short hand.

You'll never get anywhere if you just give up :).

Well that is my motivational post for the day, I can go back to being an argumentative arse now :).
 

i.shaun

macrumors 6502a
Original poster
May 1, 2008
784
0
Canada
Okay, ignoring my complete lack of knowledge as to what I'm doing, I tried again, and to my surprise it seems to kinda be working so far.


Code:
property extension_list : {"jpg", "jpeg", "tif", "tiff", "png", "gif"}
property type_list : {"JPEG", "TIFF", "PNGf", "GIFf"}
--i copied the above code from another file, I don't even know if it does anything--
on adding folder items to this_folder after receiving added_items
	tell application "Finder"
		activate
		select added_items
		set counter to "0"
		set new_item_name to "ahhh"
		
	end tell
end adding folder items to

So far when I add an item, it will:
-activate finder
-select added item

it will not re-name it, though :(

Did I type the command wrong? or is there another command to simulate double clicking the file (to re-name it, or edit it's name)
 

i.shaun

macrumors 6502a
Original poster
May 1, 2008
784
0
Canada
I thought I had a script for you but stumbled across this. Looks like this script handles a lot more than I was going to. Good luck.

mt

i was actually going for a folder action to add a number to the prefix of a file as it is dropped into the folder.

so I drop one item in, it gets "0001_originalfilename". The next item gets "0002" and so on.



I was trying to start with the basic re-naming in my last post before going with the sequential numbering, however simply re-naming a single file doesn't seem to work. If this doesn't work, I can't possibly expect to re-name multiple files as they're dropped in.
 

craig1410

macrumors 65816
Mar 22, 2007
1,130
911
Scotland
Hi,

I'm completely new to Applescript too but your question has prompted me to do a bit of learning. Here is what I came up with to solve your problem (almost):

Code:
property file_count : 0

on adding folder items to this_folder after receiving added_items
	repeat with aFile in added_items
		set prefix to pad(file_count as string)
		tell application "Finder"
			set name of file aFile to prefix & "_" & name of file aFile
		end tell
		set file_count to file_count + 1
	end repeat
end adding folder items to

on pad(s)
	repeat while length of s < 4
		set s to ("0" & s)
	end repeat
end pad

This works fairly well but there is a slight problem if you add a file and then add another file before the first has been handled. It only seems to perform the rename operation for the second of the two files added. However, if you add 2 files both at the same time it will handle that just fine.

Note that the file_count property is reset each time you compile the script. To work around this you can change the initial value of the property from 0 to whatever the last value you used. This might be handy if you use the script for a while then make a small change and don't want to be forced to start at 0000_filename.ext again.

I hope this helps, it certainly helped me to learn a small amount of applescript.

Cheers,
Craig.
 

i.shaun

macrumors 6502a
Original poster
May 1, 2008
784
0
Canada
Hi,

I'm completely new to Applescript too but your question has prompted me to do a bit of learning. Here is what I came up with to solve your problem (almost):

Code:
property file_count : 0

on adding folder items to this_folder after receiving added_items
	repeat with aFile in added_items
		set prefix to pad(file_count as string)
		tell application "Finder"
			set name of file aFile to prefix & "_" & name of file aFile
		end tell
		set file_count to file_count + 1
	end repeat
end adding folder items to

on pad(s)
	repeat while length of s < 4
		set s to ("0" & s)
	end repeat
end pad

This works fairly well but there is a slight problem if you add a file and then add another file before the first has been handled. It only seems to perform the rename operation for the second of the two files added. However, if you add 2 files both at the same time it will handle that just fine.

Note that the file_count property is reset each time you compile the script. To work around this you can change the initial value of the property from 0 to whatever the last value you used. This might be handy if you use the script for a while then make a small change and don't want to be forced to start at 0000_filename.ext again.

I hope this helps, it certainly helped me to learn a small amount of applescript.

Cheers,
Craig.

wow that code looks way different than what I had. I just tried it and it took a picture, and added "0002_0001_00_0002_originalname.ext"

then it was doing it over and over to the same file, until I moved the file out. now the name is "0005_0004_0003_0002_..."

I guess once it re-names, it thinks the new file is another one, and re-names it again. Was this one of the bugs you encountered? lol



"property file count 0" -- what does this do? Is this to tell it to start with a empty folder containing 0 files?


"repeat with aFile in added_items
set prefix to pad(file_count as string)
tell application "Finder"
set name of file aFile to prefix & "_" & name of file aFile
end tell"




telling it to repeat something with "aFile" (the added file? I don't know where you got this command)

set prefix to pad (i never saw "pad" before in other scripts, other ones had "the_counter" or other commands.)

"file_count as string" does this tell it to count the files, and use the next number in line?


set name of file aFile (seems like you are directing it to a specific file "aFile". I never would have guessed to use this command.

Anyway, can you possibly try to explain the script to me as I'm obviously lost. I thought I almost had it, and then this one is way different. This suggests that I was nowhere close and should not have even tried or gave up when I said I would in an earlier post lol
 

i.shaun

macrumors 6502a
Original poster
May 1, 2008
784
0
Canada
at least I managed to write a useful script out of all of this.

my last attempt at the script is a good folder action for the "Applications" folder. When I drop a new file in, it will automatically select the file. Normally, I'd have to find it in the list myself, to select it.

Code:
on adding folder items to this_folder after receiving added_items
	tell application "Finder"
		activate
		select added_items
		
	end tell
end adding folder items to
 

craig1410

macrumors 65816
Mar 22, 2007
1,130
911
Scotland
Hi, I'll look into the repetitive renaming issue when I get home tonight (sorry forgot to do it last night). I didn't notice this happening but I'm confident I can fix it if it does happen with me too. I'll try to explain some of your other questions.

"property file_count 0" -- what does this do? Is this to tell it to start with a empty folder containing 0 files?
By defining file_count as a property it will ensure that the number "persists" between each time the script is run and continues to increment rather than going back to zero each time. Setting it to zero here is only the initial value which it resets to each time the script is compiled (ie. if you change the script and recompile it in script editor).

"repeat with aFile in added_items
set prefix to pad(file_count as string)
tell application "Finder"
set name of file aFile to prefix & "_" & name of file aFile
end tell"

telling it to repeat something with "aFile" (the added file? I don't know where you got this command)
To be honest I copied some of this code from a tutorial (remember I'm learning Applescript too...) but I can tell you that aFile is simply a variable name which contains a reference to each file in the "added_items" collection. added_items contains all the files which you have copied into the folder which could be just one or it could be multiple. The repeat loop simply runs once for each file and that file is referenced by the aFile variable. Do you follow?

Where it says "name of file aFile" - this simply extracts the filename of the file from the aFile variable. You might think of filename and file to be one in the same but in reality the filename is simply an attribute of a given file.

set prefix to pad (i never saw "pad" before in other scripts, other ones had "the_counter" or other commands.)
pad is a function which I defined which takes a number (eg. 1) and pads it with zeros to make the number 4 digits long (eg. 0001). The definition of the function is at the bottom of the code I gave you and starts "on run...)

"file_count as string" does this tell it to count the files, and use the next number in line?
This is the parameter for the pad function. The function takes an input and produces an output. The input is simply the file_count variable contents but converted into a String data type. This is required in order for the pad function to evaluate things like the length of the value.

set name of file aFile (seems like you are directing it to a specific file "aFile". I never would have guessed to use this command.
This line is simply changing the name of the file already referenced by the aFile variable (as described above) to include the prefix which we just created and stored in the "prefix" variable.

Anyway, can you possibly try to explain the script to me as I'm obviously lost. I thought I almost had it, and then this one is way different. This suggests that I was nowhere close and should not have even tried or gave up when I said I would in an earlier post lol

Forgive me for saying this but I think you need to go back to basics and learn some programming fundamentals. It's difficult to understand programming of any kind without having some basic knowledge of variables, functions and control structures (eg. repeat, if-then-else). We've all had to do this at one stage and it's a bit like learning a new spoken language. However, in many respects, different computer languages (eg. Java, C, Applescript etc.) are more like dialects of one generic "computer language". Once you have learned the essential concepts of this generic language then you can apply this to allow you to work with other specific languages. If applescript is most relevant to you then start with that. If you ask, I'm sure someone will recommend a good book on the subject (I don't know of any myself).

Kind regards,
Craig.
:)
 

i.shaun

macrumors 6502a
Original poster
May 1, 2008
784
0
Canada
Thanks for the reply, Craig. It seems to make sense, and yes I guess I will have to look up the basics. I pretty much just jumped in, trying to "reverse engineer" other scripts and get things to work without truly knowing what each command even does, or why other commands fail to work together.


I tried reading some stuff on it, but that material seems to jump in with the assumption that you know about programming, and about applescript. They start off with codes, adding commands, getting letters that seem to come out of nowhere "repeat with i from 1 to...". I'll give it another go, searching specifically for "basics" "variables" & "control structures".

I see the if/then/else coming up a lot in some scripts, so I guess knowing what they are, what they do, when they're needed, and most importantly, how they're used would benefit me.



Also, if the script indeed works for you, it may be because if your version of OS X. I am running on Tiger 10.4.11, so that might cause the script not to work exactly the same as it does for you, if you run Leopard.
 

craig1410

macrumors 65816
Mar 22, 2007
1,130
911
Scotland
Hi,
Yes don't give up, everything will click together if you keep at it. It's a worthwhile skill to learn.

I've just had a look in my "Drop Folder" (ie. the one with the script attached) and it is still just sitting there with the files I originally put there so I'm definitely not seeing the same issue you are seeing. Maybe it is to do with me running Leopard and you running Tiger but can I just check - how have you set up the script? Are you using folder actions? I'm not sure if this is the same on Tiger but I go to Finder - Applications - Applescript - Folder Actions Setup and then create a folder on the right hand side of the resulting panel and the add item action on the right hand side. Is this what you did? I'm wondering if you are somehow running the script against the drop folder as a whole rather than just running the script for the "added" items.

If this is a Tiger thing, I agree, the best way to handle this is to move the files out of the "drop box" into a storage folder as part of the script. The only other way to handle it would be to ignore any files which already start with 4 digits and an underscore. Let me know if you need help with this as I have an iBook running Tiger and could modify the script for you. It helps me to improve my Applescript skills so it's really no trouble.

Cheers,
Craig.
 

i.shaun

macrumors 6502a
Original poster
May 1, 2008
784
0
Canada
Hi,
Yes don't give up, everything will click together if you keep at it. It's a worthwhile skill to learn.

I've just had a look in my "Drop Folder" (ie. the one with the script attached) and it is still just sitting there with the files I originally put there so I'm definitely not seeing the same issue you are seeing. Maybe it is to do with me running Leopard and you running Tiger but can I just check - how have you set up the script? Are you using folder actions? I'm not sure if this is the same on Tiger but I go to Finder - Applications - Applescript - Folder Actions Setup and then create a folder on the right hand side of the resulting panel and the add item action on the right hand side. Is this what you did? I'm wondering if you are somehow running the script against the drop folder as a whole rather than just running the script for the "added" items.

If this is a Tiger thing, I agree, the best way to handle this is to move the files out of the "drop box" into a storage folder as part of the script. The only other way to handle it would be to ignore any files which already start with 4 digits and an underscore. Let me know if you need help with this as I have an iBook running Tiger and could modify the script for you. It helps me to improve my Applescript skills so it's really no trouble.

Cheers,
Craig.

I go to the folder action configuration (Right click/Cmd+click, 'Configure Folder Actions', then I am presented with a window containing 2 panes. Both have "+" & "-" signs at the bottom for adding/removing items.

one is called "On/Folders with Actions" the other, "On/Scripts"

under the "On" sections are check boxes for any items added. The "Folders with actions" is where I press the "+" button to select a folder that I want the action attached to. I selected the photo folder I wished to drop items into, and make sure it is checked.

I then click the "+" on the other side, and select the folder action script, the one you wrote. It will now execute whenever anything is dropped into that folder. I drop a single file in, and the problem I mentioned occurs. If I drop more than 1, the problem is doubled like so:

Originals:
file.png
File2.png

Output (after a few seconds):
0000_0001_0002_0003_file.png
00004_0005_0006_0007_file2.png

If I remove them, and drop them back in, the file count drops back to 0, and it will begin again "0000_..."


This must be a Tiger glitch, and my "drop box" idea seems like it would work (i've already tried to add "move file to.." command, but it didn't work). My only concern is the file count dropping back to zero after it moves the re-named file into it's folder. The drop box's file count would drop back to zero.



There must be a way for it to *remember* where it was at with the last handled file, and failing that, maybe create an alias to the file after moving it. Aliases take less space, and would act as files, keeping the counter up.


I would appreciate if you could figure out a working script on Tiger for me, as my attempts seem to fail miserably. I still need to learn the basics, and maybe tackle less complex projects in the future before trying something like this again.

I then cl
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.