Simple "yes/no" script: help request!

I'm trying to write a simple "yes/no" script that will either open a file (in a given application) or not, depending on the user's choice. Here's what I have:

--

tell application "Finder"

set my_file to the selection as alias
set my_choice to button returned of (display dialog "Open This File with iDentify?" buttons {"Yes", "No"} default button 1)

if my_choice is {"Yes"} then
tell application "iDentify"
activate
open my_file
end tell

else
return
end if

end tell

--

However, when I click "Yes", nothing happens. Events/replies (as viewed in the script editor) don't show any errors. Any advice?

15" i5 MacBook 2.4GHz, Mac OS X (10.6.5)

Posted on Apr 6, 2011 5:22 AM

Reply
5 replies

Apr 6, 2011 6:06 AM in response to brian_c

You were super close! You just don't need the brackets around "Yes".
<pre style="width:630px;height:auto;overflow-x:auto;overflow-y:hidden;"
title="Copy this code and paste it into your Script Editor application.">
tell application "Finder"
set my_file to the selection as alias
set my_choice to button returned of (display dialog "Open This File with iDentify?" buttons {"Yes", "No"} default button 1)

if my_choice is "Yes" then
tell application "iDentify"
activate
open my_file
end tell
end if
end tell
</pre>

Hope this helps...

Apr 6, 2011 10:55 AM in response to brian_c

Two trivial changes I'd make - almost too trivial to mention but I can't resist... 🙂

set my_choice to button returned of (display dialog "Open This File with iDentify?" buttons {"Yes", "No"} default button 1)


The standard user interface puts the default action button to the right - look at any dialog in the OS and you'll see the 'cancel' button to the left and the (highlighted) 'ok' button to the right. To follow that model you should consider reversing your buttons:

set my_choice to button returned of (display dialog "Open This File with iDentify?" buttons {"No", "Yes"} default button 2)


(like I said, trivial, but anal 🙂 )

The second is a little more subjective.

You're relying on the selected items in the Finder. There could be one, or many items, and that select could include directories, volumes and files.

The line:

set my_file to the selection as alias


will fail if there are multiple items selected (you cannot make a list into an alias) or if there are no items selected.
Even if there is only one item selected you're not running any checks to see if your app can open them - I don't know what iDentity is supposed to do, so I don't know whether it will accept any file type (including directories) to know whether this is a problem or not.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Simple "yes/no" script: help request!

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