2015-04-13 1 views
0

Я хотел создать скрипт, который откроет диалоговое окно со списком кнопок, которые имеют разные ссылки, когда я нажимаю на них. Кто-нибудь знает, обладает ли эта возможность? Если нет, то могу ли я получить представление о чем-то, что будет?Как создать кнопку ссылки на Apple Script?

Примером может служить то вроде этого:
Откройте скрипт
Button1 -> Google.com
Button2 -> Aol.com
Button3 -> Yahoo.com

+0

У меня есть обновленный ответ – markhunte

ответ

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 
1

В редакторе скриптов вы можете иметь до трех кнопок.

В новом документе crtl Мышь щелкните по документу. И вы получите контекстное меню.

Перейдите в подменю Dialogs и вы увидите список вариантов.

Выберите Три кнопки три действия вариант.

И вы получите этот код, помещенный в документ.

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.

Таким образом, у вас может быть больше действий в виде элемента списка и сохранить возможность кнопки «Отмена».


UPDATE:

Современный Script Editor.app позволяет использовать Objective - C в вашем Applescript. Это делается с использованием интерфейса ApplescriptOBJC.

В Интернете есть много уроков и примеров.

Преимущество этого в том, что для задачи выше вы можете создать простое приложение из Редактора скриптов, в котором есть окно с кнопками и действиями.

Этот пример должен показать вам, как добавить любые кнопки, которые вы хотите, даже если вы не знаете, какие-либо целей - C или ApplescriptOBJC, который является языком шунтирующего между Applescript и Целью - C.

Целью сценария должен быть сохранен как Оставайтесь в открытом доступе Заявка

И затем запустите из Дока или, как я делаю из меню редактора сценариев Applescript.

Каждая кнопка связана с действием, которое также включает действие quit. Это простой способ сохранить приложение только тогда, когда оно вам нужно.

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 
Смежные вопросы