How do I create a link button on Apple Script?

2020-05-09 11:37发布

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?

An example is sort of like this:
Open up script
Button1 -> Google.com
Button2 -> Aol.com
Button3 -> Yahoo.com

3条回答
Melony?
2楼-- · 2020-05-09 12:14

In Script editor you can have up to three buttons.

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.

Choose the Three buttons three actions option.

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.


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.


UPDATE:

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.

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.

The aim of the script is to be Saved as a Stay open Application

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
查看更多
聊天终结者
3楼-- · 2020-05-09 12:23

Yes, there is the choose from list command. Try following script, just make sure the links and labels in the two lists are in corresponding order:

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
查看更多
一纸荒年 Trace。
4楼-- · 2020-05-09 12:31

For opening a single link, you can use like below,

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

查看更多
登录 后发表回答