Button actions

How do you script an action to only run when a button is pressed?

http://www.youtube.com/watch?v=Xcy4tFvYc2Y

Like that, but I don’t need to make it lock the computer or anything.

This is an easy one!

set buttonPressed to button returned of (display dialog "I will only work if you press the OK button!" buttons {"Is this one OK?", "Or is this one?"} default button 2)

if buttonPressed is "Is this one OK?" then
	--do this stuff
else
	--do this stuff, or delete the "else" to not do anything
end if

--OR...

set buttonPressed to (display dialog "I will only work if you press the OK button!" buttons {"Is this one OK?", "Or is this one?"} default button 2)

if button returned of buttonPressed is "Is this one OK?" then
	--do this stuff
else
	--do this stuff, or delete the "else" to not do anything
end if

That’s all you basically need to know, but here’s a few more things about buttons:

  1. If you only write the prompt, you will get a “Cancel” button which will stop the script, and an “OK” button which will let it keep going. The OK button will be the default button. Example:
display dialog "I will only work if you press the OK button!"

--This will run only if you press OK
  1. You can rename your Cancel button. Any other button you add will act like an OK button, because it won’t do anything.
display dialog "I will only work if you press the Keep Going button!" buttons {"Keep Going"} cancel button "Stop it!"

--This will run only if you press Keep Going

  1. You can set a default button- the button that is highlighted it blue when the dialog box appears- by adding
default button "Button Name"
--OR
default button 1 -- the number of the button, counting from the left to right, you want
  1. You can allow a user to enter text. Here’s how:
display dialog "Enter some text!" default answer "Text"

To provide a blank answer field, change it to

display dialog "Enter some text!" default answer ""
  1. You can act like the text they’re entering is a password, by adding
hidden answer true
  1. You can add icons, with “with icon stop/note/caution”
  2. You can add your own icons, by replacing stop/note/caution with the path to the file you want to use
  3. When a display dialog is inside a “Tell” command, the application you’re telling will add its own icon
Tell application "Finder"
display dialog "Dialog!"
end tell
  1. You can specify the length of time you want the dialog to stay onscreen, using
giving up after 2 --with 2 being the number of seconds I want the dialog to stay onscreen.

You should check out the standard Dictionary by going to Window > Library (or with shift-apple-L) and double-clicking StandardAdditions. You can also open any dictionary for the applications you see there, or for any other application drag it’s icon onto Script Editor’s.

Hope this was useful!

I learned some things I didn’t know.
I’ve been using like six lines to write a password dialog hahaha.
That’s much easier.

What I meant was a button on a remote or something though. Like a physical button.

I kind of got impatient and posted a second bulletin with this problem:
http://bbs.macscripter.net/viewtopic.php?id=25378c


display dialog ("this should have a personal icon") with icon "path name to .ico file"

it doesn’t work for me. It says a resource is missing. I also tried it with a .jpeg

Hi Vampire Vending Machines,

AFAIK you need to use the file path to an ICNS file (*.icns).


display dialog "Yeah, Mail" with icon (POSIX file "/Applications/Mail.app/Contents/Resources/app.icns")

You need some way to detect that the button was pressed. Applescript would need to know something on the computer that changes when the button is pressed, and the applescript would need to run all the time and check for that change… thus it’s very difficult with an applescript. From looking at the video you posted it says that it works using a 3rd party program called Proxi, and that program is what detects the button press and then activates an applescript.

Well that’s dissatisfying. I want to build it ALL myself.

That helps though.

as for .icns I don’t have any .icns files. Hopefully saving my icon through “Icon Folder Maker” will change the extension.

Model: Macbook
AppleScript: 1.5
Browser: Safari 525.18
Operating System: Mac OS X (10.5)

Like I thought. Saving it with “F.I.M.” works.

I think I’m going to get a .icns maker.
Does anybody know any good ones? (preferably free)

This won’t help you create your own icns file, but it will help you get an icns file from an icon you find on the web… if you have developer tools installed. In /Developer/Examples/AppKit/ there’s a project called ClipboardViewer. If you can open and build that project you will have the clipboard viewer application. This small application shows the contents of any clipboard.

So how can that help you? Here’s how I use it. I can download an icon from the web, such as from http://interfacelift.com/icons-mac/index.php?sort=date. Then you can Get Info on the icon in the Finder. In the Get Info window you can select and copy the icon… that places the icon on the clipboard.

Now I open ClipboardViewer. Choose “General Clipboard” as the clipboard that you want from the pop-up menu, under the pop-up menu is a couple choices so choose the ‘icns’ one and in the bottom section of the window you will see the hex code (I think) of the icns. Then in the bottom right corner of the window is a button called “save data…”, so click that and put “.incs” as the extension to the name you type.

That’s it and you’ll have a usable icns file. Of course you can’t use those web icons for commercial applications but for personal stuff on your computer it’s fine.

haha sweet.

The best thing is that I have “Seashore” (Which is like paint, but for mac), and know I can set the background to “transparent”, and then just copy it and make it an .icns.

Thanks.

making an .icns in seashore works, but select public.tiff in stead of .icns.
So I am going to see if it is actually a .icns or just a .tiff.

Well, It doesn’t work with applescript in dialog boxes, but you can set the icon of the app to it.
By “it” I mean the .icns that is called public.tiff in “ClipboardViewer”.

Well why not create the picture… and then the Finder icon of the picture file will be of the picture, so copy the Finder icon as I described and get its icns file.