11 Replies Latest reply: Nov 20, 2012 2:17 AM by Pierre L.
tubaknut Level 1 Level 1 (0 points)

Hi everyone,

 

I need to improve the workflow of archiving my digital sheet music library.

 

All file names look like this:

xx_yyy.pdf where xx is two digits and yyy is the instrument name.

 

I would like to have a script that goes through every file in a folder, and does conditional changes.

 

if xx = 00, yyy = Score

if xx = 01, yyy = SopranoCn

if xx = 02, yyy = SoloCn

 

and so on...

 

This way, I only need to manually rename my files with xx, and not the whole string.

 

Ideally, I would also like to name the files "xx-z", and have the script yield:

 

if xx-z = 02-1, then yyy = SoloCn+z = SoloCn1

if xx-z = 02-2, then yyy = SoloCn+z = SoloCn2

(...)

BUT if xx is just = 02, then to z-value be added to yyy.

 

Could somebody help me with this?

The simplest solution would possibly be to have the script look to a text-file, and just rename based on values in the file?

 

 

Regards,

Knut


OS X Mountain Lion (10.8.2)
  • 1. Re: AppleScript to append certain text, based on existing filename
    Pierre L. Level 4 Level 4 (3,860 points)

    Hi Knut,

     

    Maybe you might want to try the following script:

     

    set theFolder to choose folder

    set theTextFile to POSIX file "/Users/user_name/Desktop/Instruments.txt" -- for example

    set theText to read theTextFile

    tell application "Finder"

        set theFileNames to name of files of theFolder whose name extension is "pdf"

        repeat with thisName in theFileNames

            try

                set xx to (text 1 through 2 of thisName) as integer

                set theNewFileName to paragraph (xx + 1) of theText

                if character 3 of thisName is "-" then

                    set theNewFileName to theNewFileName & character 4 of thisName

                end if

                set name of file thisName of theFolder to theNewFileName & ".pdf"

            end try

        end repeat

    end tell

     

    The text file should look like this:

     

    Capture d’écran 2012-11-17 à 08.36.42.png

  • 2. Re: AppleScript to append certain text, based on existing filename
    tubaknut Level 1 Level 1 (0 points)

    Hello Pierre! This did exactly what I wanted - thank you so much!

     

    When trying the script, I found that I got the specs a bit wrong.

     

    When doing 02-2 i get 02_SoloCn2, but infact I might need the possibility to say 02-23.

    In this case I would like to have the result to be 02_SoloCn23.

     

    I tried changing

     

    if character 3 of thisName is "-" then

         set theNewFileName to theNewFileName & character 4 of thisName

    end if

     

    to

     

    if character 3 of thisName is "-" then

         set theNewFileName to theNewFileName & characters 4 through 5 of thisName

    end if

     

    That solved the problem for 02-23, but 02-2 became "02_SoloCn2..pdf", with two dots.

    The solution is probably quite obvious, and I'm starting to get the hang of applescript, but I seem to

    struggle with this issue.

     

    New feature?

     

    When considering the workflow, I found that I might be able to improve it further, by adding the possibility to create some archivies automatically.

     

    Skjermbilde 2012-11-17 kl. 23.47.55.png

    When the file names are correct, I make 5 zip-archives, each containing a group with a selection of these files.

    99_Cornets would include files with 01 [...] 05, 00_Horns with 06 [...] 09, and so on.

     

    This is quite cumbersome, since i now need to:

    1. Rename the files with the new script you provided.
    2. Manually create archives for the groups.
    3. Then rename the groups to have the proper naming.

     

    The whole flow would probably look something like this:

    1. Rename files based on solution in script.
    2. Create zip-archives of predefined groups of files.
    3. Rename these archives to match the file naming.

     

    The whole thing with xx at the beginning of the file names is to ease parsing when I finally complete my Ruby on rails project for online archiving.

     

    Thank you so much for your help so far!

     

    Resized image.

  • 3. Re: AppleScript to append certain text, based on existing filename
    Pierre L. Level 4 Level 4 (3,860 points)

    The following version of the script should fix the issue mentioned in the first part of your last post.

     

    set theFolder to choose folder

    set theText to read POSIX file "/Users/user_name/Desktop/Instruments.txt" -- for example

    tell application "Finder"

        repeat with thisName in (get name of files of theFolder whose name extension is "pdf")

            try

                set xx to (text 1 through 2 of thisName) as integer

                set theNewName to paragraph (xx + 1) of theText

                if character 3 of thisName is "-" then

                    repeat with k from 4 to 6

                        if character k of thisName is in "0123456789" then

                            set theNewName to theNewName & character k of thisName

                        else

                            exit repeat

                        end if

                    end repeat

                end if

                set name of file thisName of theFolder to theNewName & ".pdf"

            end try

        end repeat

    end tell

     

    As for the new feature, I'll see what can be done about it.

     

    You mention:

         99_Cornets with 01 [...] 05,

         99_Horns with 06 [...] 09

    Can you tell me what the three other groups should be (I'm not a musician).

  • 4. Re: AppleScript to append certain text, based on existing filename
    Pierre L. Level 4 Level 4 (3,860 points)

    I think I've found out how to automate the creation of the archives, starting with:

     

        set theGroups to

            {"Cornets", 1, 5}, ¬

            {"Horns", 6, 9}, ¬

            {"BariTrb", 10, 13}, ¬

            {"EuphBasses", 14, 17}, ¬

            {"Perc", 18, 18}}

     

    and so on…

     

    However, I don't actually understand why “00_Score.pdf” in your first post has become “00_Score_31_SilentNight_Gordon.pdf” in your last post. Could you be more explicit about the naming of the files you have in mind, if possible with precise examples.

  • 5. Re: AppleScript to append certain text, based on existing filename
    tubaknut Level 1 Level 1 (0 points)

    You're awesome!

     

    Sorry, I forgot to mention.

    My whole workflow was as follows:

     

    1. *Do quite a bit with Adobe Acrobat and ScanTailor*
    2. Give name ala "00_Score.pdf" to each file.
    3. Use name mangler to add e.g. "_31_SilentNight_Gordon" so that I get: "00_Score_31_SilentNight_Gordon" (31 is the archive number in the physical archive, SilentNight is a concatenation of the title, and Gordon is the last name of the composer.)
    4. Create archives.
    5. Rename archives, to include _31_SilentNight_Gordon.

     

    Ideally, the renaming could also be done with use of AppleScript, with a prompt asking for the archive ref number, title and composers last name.

     

    One important thing about the concatenation of the title, is that each word in the title should have the first letter capitalized. (In the Mid Bleak Winter -> InTheMidBleakWinter).

     

    As for the groups, in this case they would be:

     

    Cornets -> 1...5

    Horns -> 6...9

    BariTrb -> 10...14

    EuphBasses -> 15...17

    Perc -> 18

     

    Did this clarify anything?

  • 6. Re: AppleScript to append certain text, based on existing filename
    Pierre L. Level 4 Level 4 (3,860 points)

    Hi Knut,

     

    The three images below should illustrate my understanding of what you are asking for:

     

    Before running the script (just an example):

     

    Capture d’écran 2012-11-19 à 7.11.57 AM.png      Capture d’écran 2012-11-19 à 7.12.43 AM.png

     

    After running the script:

     

    Capture d’écran 2012-11-19 à 7.13.24 AM.png

     

     

    The script (beta version of course):

     

    set R to display dialog "Please enter the archive ref number, title and composer’s last name." default answer "_31_SilentNight_Gordon" buttons {"Cancel", "OK"} default button 2 with icon 1

    if button returned of R is "Cancel" then return

    set theEnd to text returned of R

     

    set theFolder to choose folder

    set theText to read POSIX file "/Users/user_name/Desktop/Instruments.txt" -- for example

    set theGroups to

        {"Cornets", 1, 5}, ¬

        {"Horns", 6, 9}, ¬

        {"BariTrb", 10, 14}, ¬

        {"EuphBasses", 15, 17}, ¬

        {"Perc", 18, 18}}

     

    tell application "Finder"

        repeat with thisName in (get name of files of theFolder whose name extension is "pdf")

            try

                set xx to (text 1 through 2 of thisName) as integer

                set theNewName to paragraph (xx + 1) of theText

                if character 3 of thisName is "-" then

                    repeat with k from 4 to 6

                        if character k of thisName is in "0123456789" then

                            set theNewName to theNewName & character k of thisName

                        else

                            exit repeat

                        end if

                    end repeat

                end if

                set name of file thisName of theFolder to theNewName & theEnd & ".pdf"

            end try

        end repeat

     

        activate

        set selection to {} -- unselect all

     

        -- Create new archives:

        delete (files of theFolder whose name extension is "zip")

     

        repeat with thisGroup in theGroups

            set theArchiveName to "99_" & item 1 of thisGroup & theEnd & ".zip"

     

            set {a, z} to {item 2, item 3} of thisGroup

            set theFiles to {} -- the group of files to archive

            set j to 0

            repeat with k from a to z

                if k < 10 then

                    set xx to "0" & k

                else

                    set xx to k as text

                end if

                set theseFiles to (files of theFolder whose name begins with xx)

                set j to j + (count theseFiles)

                copy theseFiles to the end of theFiles

            end repeat

     

            if j > 0 then -- create a new archive

                select theFiles

                set N to count files of folder theFolder

                tell application "System Events" to tell menu bar 1 of process "Finder" to ¬

                    click menu item 19 of menu 1 of menu bar item "File" --  Compress…

                repeat until (count files of folder theFolder) > N

                end repeat

                set theNewArchive to (file 1 of theFolder whose (name ends with "zip") and not (name begins with "99_"))

                set name of theNewArchive to theArchiveName

                set selection to {} -- unselect all

            end if

        end repeat

     

        clean up window of theFolder by kind

    end tell


    Please note that the script will delete any ZIP archive already present in the folder.

     

     

    Message was edited by: Pierre L.

  • 7. Re: AppleScript to append certain text, based on existing filename
    tubaknut Level 1 Level 1 (0 points)

    Wow! This looks very promising!

     

    I'm at work until 2200, local time in Norway, but I'll test it as soon as I get home.

     

    I'll get back to you soon.

     

    Knut

  • 8. Re: AppleScript to append certain text, based on existing filename
    tubaknut Level 1 Level 1 (0 points)

    Good evening, Pierre,

     

    I've now had time to test the script, and it works like a treat with regards to the file renaming.

     

    But when it comes to creating the archives, it throws an error message at me:

     

    error "System Events fikk en feil: Kan ikke hente menu bar item \"File\" of menu bar 1 of process \"Finder\"." number -1728 from menu bar item "File" of menu bar 1 of process "Finder"

     

    could it be because I use OS X localized for Norway?

     

    ### EDIT ###

    I poked a bit around, and changed the name of "menu bar item" from "File" to "Arkiv", which it's called in norwegian, but when running the script I was sent to the info dialogue. When trying to count from the top in the menu, I found create archive to be the 13th selection from the top. Changed the script accordingly, and had my printer print the whole set!

     

    For your information, I've included information from the Accessibility Inspector, for the required menu item.

     

    Skjermbilde 2012-11-19 kl. 23.33.27.png

  • 9. Re: AppleScript to append certain text, based on existing filename
    Pierre L. Level 4 Level 4 (3,860 points)

    But when it comes to creating the archives, it throws an error message at me:

     

    error "System Events fikk en feil: Kan ikke hente menu bar item \"File\" of menu bar 1 of process \"Finder\"." number -1728 from menu bar item "File" of menu bar 1 of process "Finder"

     

    could it be because I use OS X localized for Norway?

     

    That's right. I would have encountered the same problem if I had tested the script in French, as I often do. In order to make the script multilingual, you just have to replace “menu bar item "File"” with “menu bar item 3” in the script. On the other hand, I would be surprised if you had to replace 19 with another number. It's not necessary in French. However, to be sure in your case, use the following short script:

     

    tell application "Finder" to activate

    tell application "System Events" to tell process "Finder"

        name of menu items of menu 1 of menu bar item 3 of menu bar 1

    end tell

     

    The above script should return a list of all the menu items. Count to the number corresponding to the item "Compress" (including the missing value items). Finally, check out that number using the following script, replacing 19 with the number found above:

     

    tell application "Finder" to activate

    tell application "System Events" to tell process "Finder"

        name of menu item 19 of menu 1 of menu bar item 3 of menu bar 1

    end tell

  • 10. Re: AppleScript to append certain text, based on existing filename
    tubaknut Level 1 Level 1 (0 points)

    I've done it now, and found Compress to be number 21.

     

    Thank you so much for your help! This new script will save me lots of time!

  • 11. Re: AppleScript to append certain text, based on existing filename
    Pierre L. Level 4 Level 4 (3,860 points)

    My pleasure, Knut. I'm glad I could help you. Many thanks for your feedback.