Skip navigation

Select Path Apple Script Now Different Under Lion...help?

1826 Views 16 Replies Latest reply: May 11, 2012 2:35 PM by Mike Mcgee RSS
1 2 Previous Next
Mike Mcgee Calculating status...
Currently Being Moderated
Jan 12, 2012 10:34 AM

I have an AppleScript that I had been using with Leopard and Snow Leopard and it performed beatuifully.

Unfortuntely, something has changed in Lion and after upgrading my script doesn't work as desired. I'm guessing it has to be based on the way Lion "selects" a folder in the Finder, perhaps due to column view.

 

My original process would allow me to create a new folder in the Finder in the column view, or just select a folder that was already created. Once that folder was selected in the Finder, I could pull down the Script Menu to select this script.

 

For example if my path is:

Macintosh HD > (here is where I would create a folder like "Apple" for example)

 

If this was a project for a client I had worked for in the past, I would not need a new folder and instead would just highlight their already exisiting folder in the Finder (column view) and then select the script as usual.

 

The script would then create a series of subfolders within the selected folder, and also create an iCal To Do list name item based on the selected folder, and highlight the Finder folder in red. The selected folder ("Apple" for example) would be the name of the To Do item, etc. So in iCal it would have a new ToDo list called "Apple - Canada 1" etc.

 

So when launched, my script would create a folder and iCal ToDo something like this in bold:

 

Macintosh HD > Apple > APL011201 - Canada 1

 

This worked great....until Lion.

 

Lion seems to have changed the way it selects folders. I always use the column view in the Finder. (I hope that's the exact name. It's NOT the icon, list, or "image" view.) It's the three-column view.

 

The reason I bring this up is that in Lion if I used the list view the script works. But in column view my script doesn't crash, but instead of creating a folder in the desired area above, Lion creates it in the root of the hard drive. So the folder is created here, outside the selected folder:

 

Macintosh HD > APL011201 - Canada 1

 

It also ruins the iCal ToDo list as well so that instead of "Apple - Canada 1" being in the ToDo, the ToDo is "Macintosh HD - Canada 1".

It also now highlights the hard drive in red, instead of the job folder.

 

This all seems to be based on Lion not knowing the selected folder, so it performs the script in the incorrect path.
Also, the error is inconsistent. The script works as desired sometimes in Lion, even in teh column view, but mostly does not and will create the path incorrectly (shown above) in the root directory of the hard drive.

 

I'm racking my brain and can't solve this. Anyone see anything I can alter in regasrds to the Finder selection/path to get this script working again 100% under Lion? Any help is appreicated.

 

 

Below is my script:

 

-- make a folder structure at the currently selected Finder item

 

tell application "Finder" to try -- get the current selection

          set ThePath to the first item of (get selection)

          if the last character of (ThePath as text) is not ":" then -- a file

                    set ThePath to the container of ThePath

          end if

on error -- default to the desktop

          set ThePath to (get path to desktop folder)

end try

 

set JobName to text returned of (display dialog "Please enter the three letter company code:" default answer "Three Letter Job Code")

set incomingDate to (current date)

set numYear to year of incomingDate as number

set numMonth to month of incomingDate as number

 

set textMonth to (text -2 thru -1 of ("0" & numMonth as string))

set textYear to text -2 through -1 of (numYear as string)

 

 

set JobNum to text returned of (display dialog "Please enter Job Number/Date:" default answer {textMonth & textYear & "01"})

set JobLang to text returned of (display dialog "Please enter Project & Description:" default answer "Ad 1 Canada")

set JobNumName to JobName & JobNum & " - " & JobLang

 

try

          MakeFolderStructure out of {JobNumName, {"Client Originals"}, {"Fax"}, {" FORMATTED FILES"}, {" FINAL DELIVERABLES"}, {" WORD FILE"}} at ThePath

on error ErrorMessage number ErrorNumber -- oops

  activate me

  display alert "Error " & ErrorNumber message ErrorMessage

end try

 

to MakeFolderStructure out of SomeItem at SomeFolder

  (*

          make the folder structure defined in SomeItem at SomeFolder

          SomeList defines the structure:

                    nested list items create folders in the previous text item - {"A", {"B", {"C"}}} = /A/B/C

                    empty text items will create untitled folders

 

                    parameters -                              SomeItem [mixed]: the folder structure

                                                            SomeFolder [alias]: the destination folder

                    returns nothing

          *)

          set ParentFolder to SomeFolder

          if class of SomeItem is list then

                    repeat with AnItem in SomeItem

                              if class of AnItem is list then -- add subfolder(s)

  MakeFolderStructure out of AnItem at SomeFolder

                              else -- add a new child folder at the current parent

                                        tell application "Finder" to make new folder at ParentFolder with properties {name:AnItem}

                                        set SomeFolder to the result as alias

                              end if

                    end repeat

          else -- add a new (potential) parent folder

                    tell application "Finder" to make new folder at SomeFolder with properties {name:AnItem}

                    set ParentFolder to the result as alias

          end if

end MakeFolderStructure

tell application "Finder"

          set ParentFolder to selection as alias

          set the label index of ParentFolder to 2 -- 2 = red

 

  -- Adds an item in the iCal ToDo list for this job

 

  --get the name of the folder

          set the folder_name to the name of ParentFolder

 

          set summarycal to folder_name & " - " & JobLang

 

          tell application "iCal"

                    tell calendar "Waiting for Review"

  make new todo with properties {summary:summarycal}

                    end tell

          end tell

 

end tell

 

 

tell application "Finder"

          get ((ThePath as text) & JobNumName) as alias

          set the label index of the result to 2 -- 2 = red

end tell


Mac Pro (Mid 2010), Mac OS X (10.7.2)
  • red_menace Level 6 Level 6 (14,275 points)

    Yes, there is an intermittent bug with the Finder selection in Lion (for example, see this mailing list item).  As a workaround, I started checking to see if the selection is empty, and put up a choose dialog if so.

     

    Hmmm, that MakeFolderStructure handler sure does look familiar...

  • red_menace Level 6 Level 6 (14,275 points)

    The selection bug doesn't always show up, and I would have expected the desktop to be selected in your script due to the error, but you can add a couple of lines at the beginning to look for an empty selection, for example:

     

    tell application "Finder" to try -- get the current selection

      set itemList to the selection

      if itemList is {} then set itemList to {(choose folder)}

      set ThePath to the first item of itemList

      if the last character of (ThePath as text) is not ":" then -- a file

      set ThePath to the container of ThePath

      end if

    on error -- default to the desktop

      set ThePath to (get path to desktop folder)

    end try

     

    You could also avoid the bug by making your script into a service.

     

    ...and yes, a few pieces of that script look like they came from one of my replies in the macosxhints forums.

  • red_menace Level 6 Level 6 (14,275 points)

    Give this a try.

     

    Open the Automator application, and select Service for the document type.  Next, at the top of the workflow window choose "Service receives selected folders in Finder".  Now, drag a Run AppleScript action into your workflow from the Library > Utilities list and paste the following script (a slightly modified version of your posted script) into it:

     

    on run {input, parameters}

     

      set thePath to the first item of the input -- just one

     

      tell (current date)

                        set textMonth to text -2 thru -1 of ("0" & (it's month as integer)) -- leading zero

      set textYear to text -2 through -1 of (it's year as text)

      end tell

     

      set jobName to text returned of (display dialog "Please enter the three letter company code:" default answer "Three Letter Job Code")

      set jobNum to text returned of (display dialog "Please enter Job Number/Date:" default answer {textMonth & textYear & "01"})

      set jobLang to text returned of (display dialog "Please enter Project & Description:" default answer "Ad 1 Canada")

      set jobNumName to jobName & jobNum & " - " & jobLang

     

      try

      makeFolderStructure out of {jobNumName, {"Client Originals"}, {"Fax"}, {" FORMATTED FILES"}, {" FINAL DELIVERABLES"}, {" WORD FILE"}} at thePath

      on error errorMessage number errorNumber -- oops

      activate me

      display alert "Error " & errorNumber message errorMessage

      end try

     

      tell application "Finder"

      set the label index of thePath to 2 -- 2 = red

      set the folder_name to the name of thePath

      end tell

     

      -- Adds an item in the iCal ToDo list for this job

      set summarycal to folder_name & " - " & jobLang

      tell application "iCal" to tell calendar "Waiting for Review"

      make new todo with properties {summary:summarycal}

      end tell

     

      return thePath

    end run

     

     

    to makeFolderStructure out of someItem at someFolder

      (*

              make the folder structure defined in someItem at someFolder

              someList defines the structure:

                        nested list items create folders in the previous text item - {"A", {"B", {"C"}}} = /A/B/C

                        empty text items will create untitled folders

                        parameters -        someItem [mixed]: the folder structure

                                                  someFolder [alias]: the destination folder

                        returns nothing

              *)

      set parentFolder to someFolder

      if class of someItem is list then

      repeat with anItem in someItem

      if class of anItem is list then -- add subfolder(s)

      makeFolderStructure out of anItem at someFolder

      else -- add a new child folder at the current parent

      tell application "Finder" to make new folder at parentFolder with properties {name:anItem}

                                            set someFolder to the result as alias

      end if

      end repeat

      else

      tell application "Finder" to make new folder at someFolder with properties {name:someItem}

      end if

    end makeFolderStructure

     


    Save your workflow, giving it whatever name you want.  To use this service, right-click on a folder in the Finder window, and select the service from the contextual menu (note that the service will not show up if you have selected files).  This uses a slightly different mechanism to pass the selected items to your workflow, which should be more consistent.

  • red_menace Level 6 Level 6 (14,275 points)

    The variable thePath is the first thing set, so make sure you got the whole script pasted in there.  I tested the script on my machine (minus setting the iCal todo) and it works OK, although sometimes it takes a few seconds for it to get started.

  • red_menace Level 6 Level 6 (14,275 points)

    Can you post the entire contents of the Run AppleScript action you are using in the Service?

  • red_menace Level 6 Level 6 (14,275 points)

    I pasted the script over my test workflow to see if I missed something, but it works fine on my machine (I am also running 10.7.2).  Troubleshooting in Automator is a bit tricky, so you might try making a couple of modifications so that it will run in the AppleScript Editor, then you can look at the logs.  Change the first part to:

     

    on run -- {input, parameters}

      set input to {(choose folder)}

     

    If all else fails, you might try restarting the applications.

1 2 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (1)

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.