如何在 Apple Script 上创建链接按钮? [英] How do I create a link button on Apple Script?
问题描述
我想创建一个脚本,该脚本会打开一个带有按钮列表的对话框,当我单击它们时,这些按钮具有不同的链接.有谁知道applescript 是否有这种能力?如果没有,那么我能否获得有关某些内容的任何见解?
I wanted to create a script that would open up a dialog box with a list of buttons, that have different links when I click on them. Does anyone know if applescript has that capability? If not then can I get any insights on something that would?
一个例子是这样的:
打开脚本
按钮 1 -> Google.com
Button2 -> Aol.com
Button3 -> Yahoo.com
An example is sort of like this:
Open up script
Button1 -> Google.com
Button2 -> Aol.com
Button3 -> Yahoo.com
推荐答案
在脚本编辑器中,您最多可以有三个按钮.
In Script editor you can have up to three buttons.
在一个新文档 crtl 鼠标点击文档.您将获得一个上下文菜单.
In a new document crtl Mouse click on the document. And you will get a contextual menu.
转到对话框子菜单,您将看到一个选项列表.
Go to the Dialogs sub menu and you will see a list of choices.
选择三个按钮三个操作选项.
你会得到这个代码放在文档中.
And you will get this code placed in the document.
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
<小时>
然后你可以这样设置:
You would then set it up like so:
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
主要问题是按钮的限制.
The Main problem is the limit of buttons.
如果您将所有这三个都用于您的网站,您将无法执行任何其他操作;比如取消.
If you use all three for your sites you have no way of doing any other action; like Cancel.
更简单的选择是从@Zero 已回答的列表中进行选择.
The easier option is to do it as a choose from list like @Zero has answered.
通过这种方式,您可以以列表项的形式执行更多操作,并保留取消按钮的选项.
This way you can have more actions in the form of a list item and retain the option of a Cancel button.
更新:
现代 Script Editor.app 允许您在 Applescript 中使用 Objective - C.这是通过 ApplescriptOBJC 桥接语言完成的.
The Modern Script Editor.app allows you to use Objective - C in your Applescript. This is done with ApplescriptOBJC bridging language.
网上有很多课程和例子.
There are plenty of lessons and example on the web.
这样做的好处是,对于上述任务,您可以从脚本编辑器中创建一个简单的应用程序,该应用程序具有一个带有按钮和操作的窗口.
The advantage of this is that for the task above you can create a simple app from within Script Editor that has a window with buttons and actions.
这个例子应该向你展示如何添加任何你想要的按钮,即使你不知道任何 Objective - C 或 ApplescriptOBJC,这是 Applescript 和 Objective - C 之间的桥接语言.
This example should show you how to add any buttons you want, even if you do not know any Objective - C or ApplescriptOBJC which is the bridging language between Applescript and Objective - C.
脚本的目的是保存为保持打开的应用程序
然后从 Dock 运行,或者像我一样从脚本编辑器 Applescript 菜单运行.
And then run from the Dock or as I do from the Script Editor Applescript Menu.
每个按钮都链接到一个动作,其中还包括一个退出动作.这是一种让应用仅在您需要时运行的简单方法.
Each button is linked to an action which also includes a quit action. This is a simple way of keeping the app only running when you need it.
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
这篇关于如何在 Apple Script 上创建链接按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!