在脚本编辑器中,您最多可以有三个按钮。
在一个新文件中crtl 鼠标点击该文件。你会得到一个上下文菜单。
转到对话框子菜单,您将看到一个选项列表。
选择三个按钮三个动作选项。
你会得到这个代码放在文档中。
display dialog "" buttons {"", "", ""} default button 3
set the button_pressed to the button returned of the result
if the button_pressed is "" then
-- action for 1st button goes here
else if the button_pressed is "" then
-- action for 2nd button goes here
else
-- action for 3rd button goes here
end if
然后你可以这样设置:
display dialog "Choose a site" buttons {"Google", "AOL", "Yahoo"} default button 3
set the button_pressed to the button returned of the result
if the button_pressed is "Google" then
open location "http://www.google.com"
else if the button_pressed is "AOL" then
open location "http://www.aol.com"
else
open location "http://www.yahoo.com"
end if
主要问题是按钮的限制。
如果您将这三个都用于您的网站,您将无法执行任何其他操作;比如取消。
更简单的选择是从@Zero 回答的列表中进行选择。
这样您可以以列表项的形式进行更多操作,并保留取消按钮的选项。
更新:
现代 Script Editor.app 允许您在 Applescript 中使用 Objective - C。这是使用 ApplescriptOBJC 桥接语言完成的。
网上有很多课程和例子。
这样做的好处是,对于上述任务,您可以从脚本编辑器中创建一个简单的应用程序,该应用程序有一个带有按钮和操作的窗口。
这个例子应该告诉你如何添加任何你想要的按钮,即使你不知道任何 Objective - C 或 ApplescriptOBJC 是 Applescript 和 Objective - C 之间的桥接语言。
脚本的目的是保存为保持打开的应用程序
然后从 Dock 运行,或者像我从脚本编辑器 Applescript 菜单一样运行。
每个按钮都链接到一个动作,该动作还包括一个退出动作。这是一种让应用仅在需要时运行的简单方法。
use scripting additions
use framework "Foundation"
use framework "cocoa"
--- set up window
property buttonWindow : class "NSWindow"
set height to 180
set width to 200
set winRect to current application's NSMakeRect(0, 0, width, height)
set buttonWindow to current application's NSWindow's alloc()'s initWithContentRect:winRect styleMask:7 backing:2 defer:false
buttonWindow's setFrameAutosaveName:"buttonWindow"
--set up buttons
set googleButtonFrame to current application's NSMakeRect(25, (height - 40), 150, 25) -- button rect origin ,x,y ,size width,hieght
set googleBtn to current application's NSButton's alloc's initWithFrame:googleButtonFrame -- init button
googleBtn's setTitle:"Google"
set googleBtn's bezelStyle to 12 --NSRoundedBezelStyle
googleBtn's setButtonType:0 --NSMomentaryLightButton
googleBtn's setTarget:me
googleBtn's setAction:"openGoogle:"
--
set AOLButtonFrame to current application's NSMakeRect(25, (height - 80), 150, 25)
set AOLBtn to current application's NSButton's alloc's initWithFrame:AOLButtonFrame
AOLBtn's setTitle:"AOL"
set AOLBtn's bezelStyle to 12 --NSRoundedBezelStyle
AOLBtn's setButtonType:0 --NSMomentaryLightButton
AOLBtn's setTarget:me
AOLBtn's setAction:"openAOL:"
---
set yahooButtonFrame to current application's NSMakeRect(25, (height - 120), 150, 25)
set yahooBtn to current application's NSButton's alloc's initWithFrame:yahooButtonFrame
yahooBtn's setTitle:"Yahoo"
set yahooBtn's bezelStyle to 12 --NSRoundedBezelStyle
yahooBtn's setButtonType:0 --NSMomentaryLightButton
yahooBtn's setTarget:me
yahooBtn's setAction:"openYahoo:"
--
set cancelButtonFrame to current application's NSMakeRect(65, (height - 170), 75, 25)
set cancelBtn to current application's NSButton's alloc's initWithFrame:cancelButtonFrame
cancelBtn's setTitle:"Cancel"
set cancelBtn's bezelStyle to 12 --NSRoundedBezelStyle
cancelBtn's setButtonType:0 --NSMomentaryLightButton
cancelBtn's setTarget:me
cancelBtn's setAction:"terminate"
--
-- add buttons to the window
buttonWindow's contentView's addSubview:googleBtn
buttonWindow's contentView's addSubview:AOLBtn
buttonWindow's contentView's addSubview:yahooBtn
buttonWindow's contentView's addSubview:cancelBtn
-- activate the window
buttonWindow's makeKeyAndOrderFront:buttonWindow
---
on openGoogle:sender
open location "http://www.google.com"
terminate()
end openGoogle:
on openAOL:sender
open location "http://www.aol.com"
terminate()
end openAOL:
on openYahoo:sender
open location "http://www.yahoo.com"
terminate()
end openYahoo:
on terminate()
tell me to quit
end terminate