Skip navigation

AppleScript to append certain text, based on existing filename

981 Views 11 Replies Latest reply: Nov 20, 2012 2:17 AM by Pierre L. RSS
tubaknut Calculating status...
Currently Being Moderated
Nov 17, 2012 3:36 AM

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

  • 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).

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

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

  • 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

  • Pierre L. Level 4 Level 4 (3,860 points)

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

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.