如何在Apple Script上创建链接按钮?

时间:2015-04-13 02:32:28

标签: button hyperlink applescript

我想创建一个脚本,打开一个带有按钮列表的对话框,当我点击它们时,它们会有不同的链接。有谁知道Applecript是否具备这种能力?如果没有,那么我可以获得任何有关某些事情的见解吗?

一个例子是这样的:
打开脚本
Button1 - > Google.com
Button2 - > Aol.com
Button3 - > Yahoo.com

3 个答案:

答案 0 :(得分:2)

是的,有choose from list命令。尝试以下脚本,只需确保两个列表中的链接和标签按相应的顺序排列:

set listWithLinks to {"google.com", "aol.com", "yahoo.com"}
set listWithLabels to {"Google", "AOL", "Yahoo"}

set dialogTitle to "Select & Go…"
set buttonOK to "Go"
set buttonCancel to "Cancel"

set choosedLabels to choose from list (listWithLabels as list) with title dialogTitle OK button name buttonOK cancel button name buttonCancel with multiple selections allowed
if false is choosedLabels then return

repeat with i from 1 to number of items in choosedLabels
    set choosedLabel to item i of choosedLabels

    repeat with i from 1 to number of items in listWithLabels
        set lookupLabel to item i of listWithLabels
        if choosedLabel is lookupLabel then
            set link to item i of listWithLinks
            open location "http://www." & link
        end if
    end repeat

end repeat

答案 1 :(得分:1)

在脚本编辑器中,您最多可以有三个按钮。

在新文档中 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回答的列表中选择。

这样,您可以以列表项的形式执行更多操作,并保留“取消”按钮的选项。


<强>更新

Modern 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

答案 2 :(得分:0)

要打开单个链接,您可以像下面这样使用

set theAlertText to "Swiftlint is not installed"
set theAlertMessage to "Download from https://github.com/realm/SwiftLint manually. Would you like to open link?"
display alert theAlertText message theAlertMessage as critical buttons {"Cancel", "Open link"} default button "Open link" cancel button "Cancel" giving up after 60
set the button_pressed to the button returned of the result
if the button_pressed is "Open link" then
    open location "https://github.com/realm/SwiftLint/blob/master/README.md"
end if