I would like to know how to make an applescript that would set the position of a folder and all the folders in that folder. I did manage to get one folder to position but when i close the folder and reopen it, the folder doesn’t open in the position that my applescript put it. It reverts back to the place it was originally. Please can someone show me how to make this work.
This is an example of the code. when i close the folder it will not open in that position that applescript put it. Am i doing something wrong or is it not possible to set the position so it will open there the next time it is opened without the applescript.
set this_folder to choose folder with prompt "Choose folder:"
tell application "Finder"
activate
open folder this_folder
if (exists window 1) then
tell window 1
set {x1, y1, x2, y2} to get bounds
set {w, h} to {x2 - x1, y2 - y1}
set {x1, y1} to {10, 50}
set bounds to {x1, y1, x1 + w, y1 + h}
end tell
end if
end tell
There are some aspects of Finder’s behaviour in this that suggest the odd bug. However, there are also one or two workarounds - albeit not quite as elegant as we might prefer. Here’s an approach that may help:
to moveWindow for f to p
tell application "Finder" to tell f
tell (a reference to container window)
if class is Finder window then
close
update f
set position to p
open
else
set position to p
end if
end tell
repeat with i in (get folders)
my (moveWindow for i to p)
end repeat
end tell
end moveWindow
tell application "Finder" to my (moveWindow for folder (choose folder) to {10, 50})
Thanks Guys. This is exactly what i was looking for. I worked for weeks on this and couldn’t make it work. Are there any books or websites that dwell on the syntax and use of Applescript in OSX. Maybe something with the description of all the keywords and what they do.
Since I haven’t read any books on AppleScript , I can’t really recommend one in particular. However, I’ve heard good reports about: AppleScript: The Definitive Guide, AppleScript and the AppleScript Handbook. (I’m sure others will be happy to recommend their own personal favourites.)
Don’t overlook that ageing reference from Apple, the applescript language guide (often referred to as the ASLG). While this may not focus on the latest features in OS X, it does cover most of the basic (and not-so-basic) scripting principles.
Finally (and this is a point that often seems to be overlooked), don’t forget to check out the AppleScript dictionary of any application that you may be attempting to script. While dictionaries don’t include syntax examples, they can frequently give you a better idea of where to start in the guessing process…
(BTW, I’ve also tweaked my suggestion above, to rectify some sloppy scripting on my part.)
Good luck.
The example of these scripts work fine, but after running them on larger folders with many folders deep i notice that they are not setting position on all folders. Also is it possible to put in the code something to set the size of all folders to the same size. Thanks guys for all the help.
Prince:
Sorry I cannot help you with the window issue here, but believe me, kai’s advice is top notch, and now that I see he has not read any AS books, his status has just increased another couple of notches!! (Feel free to blush again, kai.)
Anyway, I am a book hound, and am also the Books/Resources admin here at MacScripter. In case you have not seen our books page, here is the link:
My two favorite texts are Stephen Kochan’s book:
http://macscripter.net/books/reviews/63_0_1_0_C/
And Hanaan Rosenthal’s:
http://macscripter.net/books/reviews/49_0_1_0_C/
You will find plenty of titles, and at least one free publication, although it may be too basic for you, on our Books page(s). Look them over, and as kai says, never forget the dictionaries. Yes, they are boring, and often unintelligible, but as long as you know the difference between a class, element, and property, you can glean useful information from them. By the way, nearly all AS dictionaries are available in PDF format here:
http://macscripter.net/app_dictionaries/
Good luck,