【问题标题】:How do I create a link button on Apple Script?如何在 Apple Script 上创建链接按钮?
【发布时间】:2015-06-18 06:36:44
【问题描述】:

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

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

【问题讨论】:

  • 我已经更新了我的答案

标签: button hyperlink applescript


【解决方案1】:

是的,有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

【讨论】:

    【解决方案2】:

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

    在一个新文件中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
    

    【讨论】:

      【解决方案3】:

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

      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
      

      【讨论】:

        猜你喜欢
        • 2011-11-25
        • 2011-08-15
        • 2022-01-25
        • 1970-01-01
        • 1970-01-01
        • 2015-04-12
        • 2018-06-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多