Apple Script Questions

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Apple Script Questions Wed Nov 05, 2008 8:19 pm • by xaMax
Hi,

I have some problems with AppleScript and Hazel.
I'm quite new to AppleScript, so I don't know, how to handle "theFile" correctly.
Code: Select all
   beep
   set fileNameOne to theFile
   display dialog "The File " & fileNameOne & " is old. Move to trash?" buttons {"Yes", "No"} default button 1
   set the button_pressed to the button returned of the result
   if the button_pressed is "Yes" then
      
      tell application "Finder"
         move file theFile to the trash
      end tell
      
   else
      set myPath to theFile
      tell application "Finder"
         set label index of (myPath as alias) to 2
      end tell
   end if


If I try this in Skripteditior (with an explizit file instead of "theFile", it works correctly. But not in Hazel.
And sometimes, if I want to aktivate a rule with an embedded script (with a failure in the script), Hazel crashes :/

Any ideas, how to correct the code?
Thanks in advance!
Bye
xaMax
 
Posts: 17
Joined: Thu May 01, 2008 8:23 am

Re: Apple Script Questions Thu Nov 06, 2008 5:37 am • by xaMax
ah, I just found this topic: viewtopic.php?f=4&t=76

the error was in the "display dialog".
Now the code works like this:
Code: Select all
....
set fileNameOne to theFile

tell application "Finder"   
display dialog "The File " & fileNameOne & " is old. Move to trash?" buttons {"Yes", "No"} default button 1
end tell

   set the button_pressed to the button returned of the result
....


but btw: is there any possibility to get the Dialog in front of every(!) program, if I use "tell application "Finder"" the Pop Up is not in front but in the back, if the Finder is not active.


and a feature request: is it possible to specify a "Script-Folder". so that everytime you choose "run applescript" all of your scripts in the specific folder are listed in the drop down menu?
xaMax
 
Posts: 17
Joined: Thu May 01, 2008 8:23 am

Re: Apple Script Questions Thu Nov 06, 2008 1:00 pm • by Mr_Noodle
You can tell the application to activate which will bring it front. The problem is that if you use Finder, this will also bring up any other Finder windows. Instead you can message "System Events" like so:
Code: Select all
tell application "System Events"
    activate
    display dialog...
end tell


Also, it looks like you are trying to stop execution if the user clicks "No". Check the in-app help (search for "AppleScript"). You need to return a record with the "hazelStop" key. You can tack the following onto the end of your script to get the desired effect:

Code: Select all
if button_pressed is "Yes" then
   return {hazelStop:true}
end if


This will tell Hazel to stop processing any more actions. I assume there is an action to throw the file away after this which is what you do not want to execute if they click no. If you don't return a record, the result is ignored and execution will continue.

Try the above and see if that helps things.

Oh, and concerning the script folder, if you select "Other" and it brings up the file dialog, it should be set to show your Library/Scripts folder initially. Also, the script pop-up will show any scripts which you are already using elsewhere.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Apple Script Questions Thu Nov 06, 2008 2:15 pm • by xaMax
Mr_Noodle wrote:You can tell the application to activate which will bring it front. The problem is that if you use Finder, this will also bring up any other Finder windows. Instead you can message "System Events" like so:
thx, that works good!

Mr_Noodle wrote:Also, it looks like you are trying to stop execution if the user clicks "No". Check the in-app help (search for "AppleScript"). You need to return a record with the "hazelStop" key. You can tack the following onto the end of your script to get the desired effect:
...
This will tell Hazel to stop processing any more actions. I assume there is an action to throw the file away after this which is what you do not want to execute if they click no. If you don't return a record, the result is ignored and execution will continue.

hm no, I think, I don't need it, because the only action in that rule is the Apple Script. If the user clicks on Yes, the File is moved to trash (via Applescript: move file thefile to the trash) and if the user click on No, the File is marked red. So I think there is no need to stop execution because there is just no other thing to execute ;)

Mr_Noodle wrote:Oh, and concerning the script folder, if you select "Other" and it brings up the file dialog, it should be set to show your Library/Scripts folder initially. Also, the script pop-up will show any scripts which you are already using elsewhere.
ah, you're right! Thanks!
xaMax
 
Posts: 17
Joined: Thu May 01, 2008 8:23 am


Return to Support