5 Replies Latest reply: Feb 10, 2013 6:32 AM by softwater
pplanch Level 1 Level 1 (20 points)

As an experienced programmer in Pascal, Excel VBA, ... I am trying to find my way in AppleScript (first steps).

 

I am trying to build a small script that will run on a keyboard shortcut, and that will toggle the label of selected files and folders (multiple selection ?) in Finder between "purple" and "no label".

 

Based on a recorded action, I was trying something along the lines of

 

tell application "Finder"

          Activate

          repeat with oneItem in selection

                         set label index of oneItem to 3

          end repeat

end tell

 

I am not clear on how to make the script understand that in a multiple selection I want to apply the requested command to each item. In addition, when testing it on one single item, it appears that I can't set the label index property of "selection" ...

 

Can someone please advise what the correct way is of (a) setting the property of a selected item in the Finder and (b) looping through a multiple selection ?

 

Thanks in advance.

 

Philip

 

P.S. Running Mac OSX 10.8.2 on MacBook Air, MacBook Pro and MacMini

  • 1. Re: AppleScript : modify labels for multiple selection of files and folders in Finder ?
    softwater Level 5 Level 5 (5,370 points)

    Something like this?

     

    set the target_file to choose file with multiple selections allowed

    repeat with i from 1 to the count of the target_file

              tell application "Finder"

                        set the label index of item i of the target_file to 5

              end tell

    end repeat

  • 2. Re: AppleScript : modify labels for multiple selection of files and folders in Finder ?
    pplanch Level 1 Level 1 (20 points)

    Actually, I was looking (and meanwhile have found it) for a solution that would not involve a dialog box. Thanks for your suggestion, helps me to better understand AppleScript syntax.

     

    The solution I found meanwhile looks like this :

     

    tell application "Finder"

              repeat with oneItem in selection as list

                        set label index of oneItem to 5

              end repeat

    end tell

     

    So, now when I select a couple of files in Finder, I run this script and the label will be set to purple for the selected files. By changing 5 to 0, it returns the label to "none".

     

    Using Automator, I then saved this as a service workflow to the Finder with a shortcut key, and now after selecting the files I want with one keystroke the labels are changed on all of them.

     

    Thanks alot for your suggestion.

  • 3. Re: AppleScript : modify labels for multiple selection of files and folders in Finder ?
    softwater Level 5 Level 5 (5,370 points)

    Nice work.

    By changing 5 to 0, it returns the label to "none".

     

    You might consider using an 'if...else' block to determine that for you so you can just use one hotkey to toggle:

     

     

    tell application "Finder"

              repeat with oneItem in selection as list

                        get label index of oneItem

                        if label index of the oneItem is 0 then

                                  set the label index of oneItem to 5

                        else

                                  set the label index of oneItem to 0

                        end if

              end repeat

    end tell

  • 4. Re: AppleScript : modify labels for multiple selection of files and folders in Finder ?
    pplanch Level 1 Level 1 (20 points)

    Thanks alot, Softwater. In fact, I have been playing around with that and found that the "get" statement in your script suggestion is not even necessary. The script I saved as a Finder Service is now :

     

    tell application "Finder"

              repeat with oneItem in selection as list

                             if label index of oneItem = 0 then

                                            set label index of oneItem to 5

                             else

                                            set label index of oneItem to 0

                             end if

              end repeat

    end tell

     

    By assigning a shortcut key to this one, I can now toggle labels.

     

    Additional trick I did is to restrict the applicability of the script to only movie-files (I am using this to change the labels of movies / TV shows on my computer which I still want to see versus the ones that I don't want to see anymore). I noticed during testing that the script would also color folders and drives selected in other finder windows than the one I was working in, so now it only works on movie files which is more what I want.

     

    Great tool, the AppleScript Editor and the Automator. Only a little bit a pitty that finding out about object structure and properties is not very straightforward in the AppleScript Editor (comparing to e.g. the VBA in MS Excel, which has an almost perfect help system and built-in reference). I am not a Microsoft fan in general, but when it comes to MS Excel VBA, I have not seen any built-in help system yet that approaches its usefulness ...

     

    I guess I will be "seeing" you again on this forum ... I am getting the taste of scripting again :-D

  • 5. Re: AppleScript : modify labels for multiple selection of files and folders in Finder ?
    softwater Level 5 Level 5 (5,370 points)

    pplanch wrote:

     

    (I am using this to change the labels of movies / TV shows on my computer which I still want to see versus the ones that I don't want to see anymore).

     

    Funnily enough someone else on the forum was asking about having a script that would automatically track seen movies in a folder and change the label (i.e., with no user intervention) after it had been opened in the movie viewer.

     

    Something like this, with a few tweaks here and there, running from a lauch daemon on a specified folder might just do the job.