(Sorry if you've read this post already. However, Yesterday, I wrote this up (it took me all day) but then, I had the sudden inspiration for a different post. So, I wrote up yesterday's post in a couple of minutes. Anyway, here's yesterday's original post....)
Yesterday, I was writing an applescript that would save my clipboards. Thus, if I accidently copied over something, I wouldn't lose it. To do this, I had to create two lists, one that had the first 30 characters of the clipboards, and one that had the entire clipboard. This way, you only see a short snippet of the clipboard when choosing which one to revert to. With this in mind, here's some questions I had while writing this, and the answers that I can up with.
If you would like, you can download the applescript I was writing from MediaFire.
How do I add an item to a list.
To do this isn't really all that hard. I just couldn't find out how to do it. I found that the best way to add an item to the list use to use:
set new_list to ((every item in current_list) & new_item)
set current_list to (new_list as list)
Naturally, replace current_list with the identifier for the list you want to add to and new_item for the item you want to add.
How do you have a "giving up after" command with a "choose from list"?
I was using:
set chooseitem to choose from list current_list OK button name "Go On" cancel button name "Go Back"
as my choose from list. However, with lists, you can't add:
giving up after 10
to make the dialog box vanish after 10 seconds. So, the solution is to first ask the user if they want to choose from the list. Then, add the giving up phrase to the dialog box. For example:
set choosefromlistq to display dialog "Choose from list?" buttons {"Not Yet", "OK"} default button 2 giving up after 10
if gave up of result then
set choosefromlist to "Not Yet"
else
set choosefromlist to button returned of choosefromlistq
end if
if choosefromlist is equal to "OK" then
set chooseitem to choose from list current_list OK button name "Go On" cancel button name "Go Back"
end if
Now, it will give up after 10 seconds. And, when it gives up, it will return the "Not Yet" text. However, I can just hit return to choose from the list. Not elegant, but the only solution I found.
My application is on repeat. How do I make a Quit item for my list selection?
This one is pretty easy to do, I had just forgotten. Instead of being able to do something like:
set chooseitem to choose from list current_list OK button name "Go On" cancel button name "Go Back"
set choice_button to button returned of chooseitem
if choice_button is "Go Back" then
return
end if
(which actually make sense), you have to do:
set chooseitem to choose from list current_list OK button name "Go On" cancel button name "Go Back"
if chooseitem is false then
return
end if
See? Not hard. Just... different. (Then what would actually make some sense.) Note that the "return" command is actually the quit command. If your app is repeating itself, then you need to use:
return
exit repeat
Because of the design of my application, I need to get what number in the list the selection is. How do I do this?
This one caused me some major problems. However, I finally came up with this little factiod: The "choose from list" does not have a way to find out what position in the list the item you selected has." Translated into shortspeak, "Choose from list isn't all that great." However, there is a way around this little problem. Using something like:
set item_num to ""
repeat with user_choice in chooseitem
repeat with i from (count current_list) to 1
if contents of user_choice is item i of previewclips then set item_num to i
end repeat
end repeat
Now, all I have to do is refer to the corresponding item in the other list with:
set the_other_item to ((item item_num of the_other_list))
Lastly, I have a dialog box that pops up, but, it has a giving up phrase. If the application is hidden, when the dialog box appears, my computer beeps and the dock icon bounces. How do I stop this?
This one is probably one of the greatest problems I had. I could not think of a solution, or find one. However, I eventually did find the solution (obviously). So, here's how you can stop the bouncing icon in the dock for an applescript application dialog box. (I put that in there so that people looking for this can find it with (hopefully) greater ease than I did. ;D )
What you'll need to do is add a few simple lines of code.
repeat
tell application "System Events" to set FRontApp to name of (first application process whose frontmost is true)
if FRontApp is "Clipboard Restorer" then
--the dialog box you want to add.
exit repeat
end if
end repeat
Now, the dialog box won't pop up until the application is your current application. (The frontmost.)
Well, that's all for now. My apologies for not linking to all the great places that helped me out. I would, but I've gone to so many applescript webpages recently that I could never find the ones that actually helped me. Also, sorry for such a long post, I didn't want to break this down into 5 different days. However, if you have any questions, don't be afraid to email me! I'd be more than glad to help. (My email is mactipper@gmail.com.)
MacTipper Recommends Dropbox!

1/23/08
Some AppleScript Questions and the Answers To Them
Posted by
Oliver
at
Wednesday, January 23, 2008
Labels: AppleScript
Subscribe to:
Post Comments (Atom)

0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.