![]() |
just to tweak out snoobies script a bit:
Code:
tell application "Finder" |
Hello TW, thanks for the improvements to the script. That is even better.
I have been playing with it even further, and made two duplicates of it, so that one script will append a space -- instead of an underscore -- and will precede the number with a zero. The second script does the same thing, except it precedes the number with two zeros. What I am trying to do now, is figure out how to tell the scripts when to, and when not to, add the leading zeros. In other words, with the 2-digit script, I want a zero to precede the first nine copies, so that we have 01 to 09. However, from 10 onward to 99, I don't want the zero appended, so that we have something like this: FileName 01 FileName 02 FileName 03 FileName 04 FileName 05 FileName 06 FileName 07 FileName 08 FileName 09 FileName 10 FileName 11 FileName 12 FileName 13 etc... FileName 20 FileName 21 FileName 22 FileName 23 etc... In the case of the 3-digit script, I want two zeros to precede the first nine copies, so that we have 001 to 009. However, from 10 onward to 99, I only want one zero appended, so that we have something like this: FileName 001 FileName 002 FileName 003 FileName 004 FileName 005 FileName 006 FileName 007 FileName 008 FileName 009 FileName 010 FileName 011 FileName 012 FileName 013 etc... FileName 020 FileName 021 FileName 022 FileName 023 etc... FileName 090 FileName 091 FileName 092 FileName 093 etc... You get it? I don't have a lot of experience with the AppleScript language, but I imagine that you need to use some kind of "if" and "else" statements, something like this with the 2-digit script: if i = 1 to 9 then FileName = & " 0" & i if i => 10 then FileName = & " " & i In the case of the 3-digit script, I imagine it would be something similar: if i = 1 to 9 then FileName = & " 00" & i if i => 10 then FileName = & " 0" & i Can you -- or someone else -- help me to refine this so that it will work? It would be great if all three scripts could be combined into one script, so that it would first ask me how many zeros to append -- 0, 1 or 2 -- and then do the necessary duplicating and renaming. 0 would result in: FileName 1 FileName 2 FileName 3 FileName 4 FileName 5 FileName 6 FileName 7 FileName 8 FileName 9 FileName 10 1 would result in: FileName 01 FileName 02 FileName 03 etc... FileName 10 FileName 11 FileName 12 FileName 13 etc... 2 would result in: FileName 001 FileName 002 FileName 003 etc... FileName 010 FileName 011 FileName 012 etc... FileName 990 FileName 991 FileName 992 FileName 993 etc... Thanks! This would be a very useful script to upload to places like VersionTracker. To my knowledge, there is nothing like this there now, unless you buy something like FileBuddy. |
This should do it for you:
Code:
// pad string s to length l |
Okay, exactly where would I insert that new code into the original code that you gave me earlier?
Is that new code going to cause the script to ask me how many, if any, zeros to pad the numbers with? Or are you saying that I need to make different scripts to get different results? Thanks again. |
Sorry, my goof. It was TW who gave me the earlier code. Are you saying that I should add your code somewhere to his code?
Thanks! |
you can use LV's zero padding subroutine, which will work, but I usually use this approach (replace the set name of theDupe... line with these two lines):
Code:
-- use -2 or two places, -3 for three places, etc.if you want the script to ask whether you want 2 or 3 (or more) digit padding, add this bit near the beginning of the script: Code:
display dialog "How many digits do you want to pad the file index?" buttons {"Cancel", "OK"} default button "OK" default answer "2"Code:
set idx to characters -the_digits thru -1 of ("00000" & i ) as text |
If you used my code example, you would simply add it to the end of the script then call it within the script where you need it. so using tw's example:
Code:
set name of theDupe to theFileName & " " & pad(i,2)This is called a subroutine or "function". It allows you to use similar code from various places without repeating the same code over and over. |
Okay...let me address TW first since he responded first.
Based on what you've said, and if I've understood you correctly, my entire script now looks like this: ---------- tell application "Finder" set theFiles to the selection end tell display dialog "How many times do you want the duplicator to run?" buttons {"Cancel", "OK"} default button "OK" default answer "" set the_loops to text returned of the result as number display dialog "How many digits do you want to pad the file index?" buttons {"Cancel", "OK"} default button "OK" default answer "2" set the_digits to text returned of the result as number tell application "Finder" repeat with thisFile in theFiles set theFileName to name of thisFile repeat with i from 1 to the_loops set theDupe to duplicate selection -- use -2 or two places, -3 for three places, etc. set idx to characters -the_digits thru -1 of ("00000" & i) as text set name of theDupe to theFileName & " " & idx end repeat end repeat end tell ---------- When I select a file called "test.txt", tell it to make 20 copies, and to pad with 1 zero, here is what I get: Test.txt Test.txt copy Test.txt 0 Test.txt 1 Test.txt 2 Test.txt 3 Test.txt 4 Test.txt 5 Test.txt 6 Test.txt 7 Test.txt 8 Test.txt 9 What I expected it to do was to place a zero before the 1 through 9 items, and then continue with 10 through 20, so that it looks like this: Test.txt Test.txt 01 Test.txt 02 Test.txt 03 Test.txt 04 Test.txt 05 Test.txt 06 Test.txt 07 Test.txt 08 Test.txt 09 Test.txt 10 Test.txt 11 Test.txt 12 Test.txt 13 etc... Okay...I figured it out. I have to select the default answer of 2 to get the above results. I thought I was just supposed to tell it how many zeros to add, which is why I said 1. I just tried padding with three, and it also worked fine. :) Las_Vegas, I'll try your method now and see what kind of results I get. It seems with your code though, that I will have to make different scripts, depending on how much padding I want to do, right? Well...let me see... Okay, maybe I am doing something wrong, Las_Vegas, but no matter where I try to stick that chunk of code, when I try to compile, I get the error "expected "end" but found "/"" and the first "/" is selected by the script editor. Also, can you add something to your code so that, similar to TW's code, it will prompt me for both how many copies, as well as how much padding to do? That way I won't have to have one script for double digits, and another for triple digits. Thanks! |
Okay Las_Vegas, I assumed that the "//" was actually a comment line, and not really a part of the code, so I removed it, and placed the rest of the code after the final "end tell" like this:
tell application "Finder" set theFiles to the selection end tell display dialog "How many times do you want the duplicator to run?" buttons {"Cancel", "OK"} default button "OK" default answer "" set the_loops to text returned of the result as number tell application "Finder" repeat with thisFile in theFiles set theFileName to name of thisFile repeat with i from 1 to the_loops set theDupe to duplicate selection set name of theDupe to theFileName & " " & pad(i, 2) end repeat end repeat end tell on pad(s, l) repeat while length of s < l set s to ("0" & s) end repeat return s end pad When I do that, the code does compile; however, when I select the same file -- test.txt -- and run the script, and tell it to make 20 copies, all it does is make one copy called "test copy.txt" and I get the error "Finder got an error: Can't continue pad". |
LV got confused and used javascript notation '//' for comments, when he should have used applescript notation '--'. I do that all the time myself; why applescript needed it's own funky comment style is beyond me... :rolleyes: just replace the // with --
|
Yes, I already figured out that was a small error on his part; but even when I replace // with -- and add that string back into the code, it obviously makes no difference. The Finder is still throwing me that same error "Can't continue pad".
|
Quote:
|
TW, I think that I'll stick with your modifications to the previous code, as it is working fine.
Las_Vegas' code is still throwing me an error even after I change it to "my pad". When I tell the modified script to make 20 copies, it just makes one copy called "test copy.txt", and then the script editor gives me the error "can't get length of 1" with the "1" highlighted in this string in the code: repeat while length of s < l I've got a script that now does what I want, thanks to help from several of you here, so maybe we should just leave it at that. Again, it would be a nice gesture if someone could create a custom icon for it, and then upload it as a free Applescript to VersionTracker, Mac Update, etc. I'm calling my personal copy "File Duplicator & Multi-Digit Renamer". :) |
Quote:
Quote:
plus it's such a dinky little single-purpose script... |
Now it's working properly. :)
So with this version of the script though, you would have to actually edit that number if you want to pad with a different amount of zeros, or else make several different versions of the script; correct? It may be a "dinky little single-purpose script", but you'd be surprised by how much people look for such little apps. I looked all over the web to find something like this, but couldn't find anything, which is how I ended up here asking about it...and now we have a script. :) Of course, you could make it less "dinky" by adding a few more features to it, such as, for example, the ability to completely change the name of the new files. Have the script prompt the user for a new name. You could also have the script prompt the user for a new location to save the copies. There's all kinds of little things you could do to spice it up a little, without investing a lot of time into it. Make it like a free mini-version of FileBuddy. :) Just a thought. BTW, I do have a few things on VersionTracker, and could stick it on my server, but I don't have my web server online all the time, since I am now down to using one ten-year-old computer. |
Oh, BTW, while FileBuddy can sequentially rename files, to my knowledge, it cannot actually duplicate and rename files. I wrote to the author of that program a few days ago about adding the duplication function to his program, and he has yet to respond. So as you can see, for the time being, at least, this "dinky" Applescript might be useful to some people. Also, if folks can get the function free -- via this script -- why would they even want to pay for it later if some developer does add it to their commercial program? :)
|
| All times are GMT -5. The time now is 09:18 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.