The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   Applications (http://hintsforums.macworld.com/forumdisplay.php?f=5)
-   -   Program To Make Multiple Copies Of A File (http://hintsforums.macworld.com/showthread.php?t=106197)

tw 10-13-2009 12:31 AM

just to tweak out snoobies script a bit:
Code:

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 thisFile
                        set name of theDupe to theFileName & "_" & i
                end repeat
        end repeat
end tell

This renames the files as you go (by using the original filename and adding a "_#" on the end). also, it allows you to select more than one original file, and will rename them all correctly.

OSXdude 10-13-2009 02:14 AM

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.

Las_Vegas 10-13-2009 06:23 AM

This should do it for you:
Code:

// pad string s to length l
on pad(s,l)
  repeat while length of s < l
      set s to ("0" & s)
  end repeat
  return s
end pad

So that pad("3",3) would result in "003" and pad("64",4) would result in "0064"

OSXdude 10-13-2009 07:45 AM

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.

OSXdude 10-13-2009 07:49 AM

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!

tw 10-13-2009 12:10 PM

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.
set idx to characters -2 thru -1 of ("000" & i ) as text
set name of theDupe to theFileName & " " & idx

the set idx... line creates a text string from the number, led with a lot of zeros - it will look like "0004" or "00027" - and then it grabs the last two characters ("04" or "27") to produce a zero-padded numeric string. no repeats or if statements necessary.

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"
set the_digits to text returned of the result as number

and change the set idx... line to read:
Code:

set idx to characters -the_digits thru -1 of ("00000" & i ) as text
though really, you should pick a consistent style and stick with it, rather than changing it on a case-by-case basis. it will be less crazy-making down the road.

Las_Vegas 10-13-2009 12:32 PM

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)
Of course, if you wanted a 3 digit number, you would use 3 rather than 2 above.

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.

OSXdude 10-13-2009 02:26 PM

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!

OSXdude 10-13-2009 02:38 PM

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".

tw 10-13-2009 02:38 PM

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 --

OSXdude 10-13-2009 02:44 PM

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".

tw 10-13-2009 02:59 PM

Quote:

Originally Posted by OSXdude (Post 557130)
The Finder is still throwing me that same error "Can't continue pad".

ah, yeah, I forgot you were in a finder tell block. try set name of theDupe to theFileName & " " & (my pad(i, 2))

OSXdude 10-13-2009 03:22 PM

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". :)

tw 10-13-2009 03:36 PM

Quote:

Originally Posted by OSXdude (Post 557136)
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

you probably need to coerce it to a string explicitly. (my pad(i as text, 2)) would probably do it.

Quote:

Originally Posted by OSXdude (Post 557136)
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". :)

that would be easy enough to do, but does version tracker have file storage? I don't have a website, personally, and I was under the impression that VT only linked to other people's download sites.

plus it's such a dinky little single-purpose script...

OSXdude 10-13-2009 03:53 PM

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.

OSXdude 10-13-2009 04:09 PM

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.