Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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

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


activate me


display alert "Error " & ErrorNumbermessageErrorMessage

end try


to MakeFolderStructureout ofSomeItematSomeFolder


(*

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)


MakeFolderStructureout ofAnItematSomeFolder

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

tell application "Finder" to makenewfolderatParentFolderwith 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 makenewfolderatSomeFolderwith 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"


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

Posted on Jan 12, 2012 10:34 AM

Reply
16 replies

Jan 12, 2012 11:51 AM in response to red_menace

Red Menace, thank you very much for your reply.


Argh, it's a Lion bug? I thought it would just require a tweak to a line of code. Darn.

I use this script multiple times a day so it's a real pain.


As for the MakeFolderStructure handler, did you create this? If so, that's great and thank you!

I really don't know AppleScript very well at all, but I was able to google some example scripts out there and combine them into one script that met my needs.


As for your workaround of checking to see if the selection is empty, is there some easy code I can insert into my script above to do this? I would love to add this as well but just don't know how.


Any help is apprecaited. Thanks again.

Jan 12, 2012 12:24 PM in response to Mike Mcgee

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:


tellapplication"Finder"totry-- get the current selection


setitemListtotheselection


ifitemListis {} thensetitemListto {(choose folder)}


setThePathtothefirstitemofitemList


ifthelastcharacterof (ThePathastext) isnot":"then-- a file


setThePathtothecontainerofThePath


endif

onerror-- default to the desktop


setThePathto (getpath todesktop folder)

endtry


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.

Jan 12, 2012 1:00 PM in response to red_menace

Red Menace,


Thanks again for your prompt and detailed reply. I really do appreicate it.


Funny thing is that I copy/pasted your updated finder portion above into my script and it still suffered from the same error. (It will create folders on the root of the hard drive) There was no difference. Still has the same Lion bug.


Even though the script shows that if there's an error it will create the folders on the desktop, my script has never done this.


I'm a total AppleScript newbie, but I have a feeling the reason my script doesn't create folders on the desktop ever is that the selection bug isn't a true script error. It's not finding an empty selection, but rather the Finder error is selecting the wrong item. Thus, it defaults to the wrong HD root folder instead of the final selected folder of the path.


That would also possibly explain why a patch of checking to see if the selection is empty, didn't change anything since it's not empty but rather just incorrect.


Argh. Lion...

Jan 12, 2012 2:03 PM in response to Mike Mcgee

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:


onrun {input, parameters}



setthePathtothefirstitemoftheinput-- just one



tell (current date)

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


settextYeartotext-2through-1of (it's yearastext)


endtell



setjobNametotext returnedof (display dialog"Please enter the three letter company code:"default answer"Three Letter Job Code")


setjobNumtotext returnedof (display dialog"Please enter Job Number/Date:"default answer {textMonth & textYear & "01"})


setjobLangtotext returnedof (display dialog"Please enter Project & Description:"default answer"Ad 1 Canada")


setjobNumNametojobName & jobNum & " - " & jobLang



try


makeFolderStructureout of {jobNumName, {"Client Originals"}, {"Fax"}, {" FORMATTED FILES"}, {" FINAL DELIVERABLES"}, {" WORD FILE"}} atthePath


onerrorerrorMessagenumbererrorNumber-- oops


activateme


display alert"Error " & errorNumbermessageerrorMessage


endtry



tellapplication"Finder"


setthelabel indexofthePathto2-- 2 = red


setthefolder_nametothenameofthePath


endtell



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


setsummarycaltofolder_name & " - " & jobLang


tellapplication"iCal"totellcalendar"Waiting for Review"


makenewtodowith properties {summary:summarycal}


endtell



returnthePath

endrun



tomakeFolderStructureout ofsomeItematsomeFolder


(*

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

*)


setparentFoldertosomeFolder


ifclassofsomeItemislistthen


repeatwithanIteminsomeItem


ifclassofanItemislistthen-- add subfolder(s)


makeFolderStructureout ofanItematsomeFolder


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


tellapplication"Finder"tomakenewfolderatparentFolderwith properties {name:anItem}

set someFolder to the result as alias


endif


endrepeat


else


tellapplication"Finder"tomakenewfolderatsomeFolderwith properties {name:someItem}


endif

endmakeFolderStructure


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.

Jan 12, 2012 2:47 PM in response to red_menace

Red Menace, I always need to start out by thanking you for your time, but this time it's especially true. I really appreciate you modifying the code and explaining the process of creating a Service.


I did all of the above, and the service launches correctly, but then stops on the script half way through.


When I run the debugger in Automator on the script you included above, it shows Error -2753 with The variable thePath is not defined with this location of the script highlighted (the "2" is highlighted) with the error line (in bold below):


---

tell application "Finder"

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

set the folder_name to the name of thePath

end tell

---


The concept of creating a Service makes perfect sense since a right-click of the folder would make the folder selection absolute. I'm just stuck on this error above. Any help is appreciated. Thanks again.

Jan 12, 2012 3:15 PM in response to red_menace

Red Menace, thanks again for your reply.


I definitely followed the steps and copied all lines of the script. The right-click starts the script by asking the three digit company code, date/job number, and project description. It then creates the five subfolders, and after that it chokes with the following Finder error:


User uploaded file


It doesn't highlight the folder in red, and using the debug it definitely is choking on this part:


---

tell application "Finder"

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

set the folder_name to the name of thePath

end tell

---


The error shows up in the Finder and The Automator debug run test. Not sure why.

Jan 12, 2012 3:22 PM in response to red_menace

Sure, thanks Red Menace.


As you noted I made this:

Service receives selected folders in the Finder.app


Then I just pasted your entire script you posted:


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


activate me


display alert "Error " & errorNumbermessageerrorMessage

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"


makenewtodowith properties {summary:summarycal}

end tell


return thePath

end run



to makeFolderStructureout ofsomeItematsomeFolder


(*

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)


makeFolderStructureout ofanItematsomeFolder

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

tell application "Finder" to makenewfolderatparentFolderwith properties {name:anItem}

set someFolder to the result as alias

end if

end repeat

else

tell application "Finder" to makenewfolderatsomeFolderwith properties {name:someItem}

end if

end makeFolderStructure

Jan 12, 2012 3:52 PM in response to Mike Mcgee

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:


onrun-- {input, parameters}


setinputto {(choose folder)}


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

Jan 12, 2012 4:37 PM in response to red_menace

Red Menace, very strange. I can't get it to work with the same code. Keep getting the same error.

In any event, I think I'll just wait until hopefully the Lion bug is fixed (if ever.)

Argh, if I wasn't in love with Lion's mail.app, I would just revert to SL.


But once again, I sincerely appreciate your time and help in assisting me with this.

It's much appreciated! Thank you!

May 11, 2012 3:27 AM in response to red_menace

red_menace, first off I want to thank you again for your help with this.


I'm resurecting this thread because your Automator serivce post actually did solve my problem! I found the error of my ways and just wanted to post this. In the iCal step in the Automator script step, I'm so stupid that my iCal calendar wasn't an *exact* match with the calendar name in the script. That was the bug. The script step above had "Waiting for Review" and my exact iCal calendar was actually called "Waiting for a Review". I completely missed this!


Your automator script does everything perfectly. There is only one simple (hopefully) thing that differs from my original AppleScript:


In my original AppleScript, the Finder would label both the parent folder in RED as well as label the newly created Folder in RED as well. The last step in my AppleScript was:


tell application "Finder"

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

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

end tell


With the automator service, only the parent folder is in RED.


Is there a simple tweak to make the resulting folder labelled in RED too?

(I tried adding this to the end of the automator Service script and it didn't work.)


Any help is appreciated, and thanks again for your assistance!

May 11, 2012 5:41 AM in response to Mike Mcgee

Looks like in our travels one of the label changes got left behind. In the last script posted, only the original item (thePath) label is being changed, so just add the other folder that was getting labeled (JobNumName), for example:


tellapplication"Finder"


# parent folder


setthelabel indexofthePathto2-- 2 = red


setthefolder_nametothenameofthePath



# child folder


get ((thePathastext) & JobNumName) asalias


setthelabel indexoftheresultto2-- 2 = red

endtell

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

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.