2014-10-05 88 views
4

OS X 10.10(優勝美地)之前,我可以保證一個AppleScript對話框告訴「當前應用程序」來激活獲得焦點:如何確保OS X 10.10(優勝美地)的Applescript對話框重點?

tell current application 
     activate 
     set output to (do shell script "printf '" & hostsLine & commentString & "' >> /private/etc/hosts" with administrator privileges) 
end tell 

這將使密碼對話框到前面鍵入的對話框。不再出現焦點在優勝美地,你必須點擊它才能打字。

在堆棧交換和其他地方搜索沒有提供任何解決方案/解決方法。

有沒有人有辦法在OS X 10.10?

更新1:萬一其他人遇到這個問題,它似乎是一個新的沙盒問題。非密碼對話框正確地獲得焦點。事實上,在「使用管理員權限」密碼對話框之前的非密碼對話框將導致密碼對話框也有焦點。我添加了一個對話框來確認Safari在測試時仍然是前端應用程序,並發現了這一點。作爲一種解決方法,我添加了超時1(秒)的前面的對話框,直到找到更好的解決方案。

tell me -- alternate method: tell current application 
    activate 
    display dialog "Trying to get focus…" with title "Can't focus in Yosemite" buttons {"Cancel", "Idle"} cancel button "Cancel" giving up after (1) 
    set output to (do shell script "printf '" & hostsLine & commentString & "' >> /etc/hosts" with administrator privileges) 
end tell 

有趣的是,BBEdit中的身份驗證保存幫手腳本(MAS的版本了)使用「具有管理員權限的」密碼對話框。但它在優勝美地得到了正確的關注。本地應用程序調用applescript與Applescript之間有什麼不同,它告訴應用程序如何執行此操作?

on documentShouldFinalizeAuthenticatedSave(theDocument, tempFilePath, destinationPath) 

    -- on input: tempFilePath points to the contents of the document written to a temp file, ready to move to the destination; destinationPath is where the file should be copied. 

    -- on exit: if the operation succeeded, delete the temp file (or else the application will assume the operation failed) and return YES for success 

    -- this is pretty straightforward: "cp tmpFilePath destinationPath" 

    do shell script "cp" & " " & quoted form of tempFilePath & " " & quoted form of destinationPath with administrator privileges 

    -- now remove the temp file, this indicates to the application that we did the work 

    do shell script "rm" & " " & quoted form of tempFilePath 

    return true 
end documentShouldFinalizeAuthenticatedSave 

更新2:如果有人好奇,一個Safari瀏覽器助手腳本的這部分我老婆阻止垃圾郵件的彈出廣告網站,如mackeeper.zeobit.com有模態對話框。編輯/ etc/hosts有點太複雜/艱鉅,她想要一個交鑰匙解決方案。如果有興趣,我可以發佈整個腳本。它在10.9以下工作正常,但密碼問題的焦點在10.10中很煩人。

+0

我認爲蘋果在10.11版中修復的是10.10.x中的一個bug。無論哪種情況,從工作10.9版本未修改的腳本,在10.11再次運行。以防萬一任何人需要另一個升級的理由。 ;) – 2015-10-30 00:03:35

回答

0

似乎是10.10.x中的一個錯誤,很可能是由於沙盒造成的。在10.11(El Cap)中修復,因爲未經修改的腳本再次像命令那樣從10.9及更早版本獲得焦點。

0

你可以嘗試...告訴我,而不是告訴當前的應用程序。 「告訴我」基本上告訴蘋果腳本激活並運行「do shell腳本」命令。這更有意義。 「do shell腳本」是一個applescript命令,因此請求applescript運行它是有意義的。也許這會幫助你解決問題。

祝你好運。

+0

感謝您的反饋。我測試了你的方法,但不幸的是它不能比「告訴當前應用程序」的方法更好。該對話框仍然沒有得到重點。 – 2014-10-06 02:19:18

+0

注意:無論我使用tell current應用程序還是告訴我,密碼對話框都能正確識別osascript正在請求管理員權限。 – 2014-10-06 16:09:54

0

個人而言,我會刪除輸入密碼的必要。最簡單的方法是將以下行添加到/ private/etc/sudoers文件中。

wife_user_name ALL=(ALL) NOPASSWD:ALL 

要刪除需要爲所有管理員帳戶密碼,從

%admin ALL=(ALL) ALL 

在/私人的/ etc/sudoers文件更改以下行來

%admin ALL=(ALL) NOPASSWD: ALL 

接下來,從

set output to (do shell script "printf '" & hostsLine & commentString & "' >> /private/etc/hosts" with administrator privileges) 
更改您的蘋果燈線

set output to (do shell script "sudo printf '" & hostsLine & commentString & "' >> /private/etc/hosts") 

更改到/私人的/ etc/sudoers文件可以通過使用終端和文本編輯應用程序來完成。打開終端應用程序並鍵入以下命令:

cd ~/desktop 
sudo cp -n /etc/sudoers /etc/sudoers.orignal 
sudo cp /etc/sudoers sudoers.txt 
sudo chmod ug+w sudoers.txt 
open sudoers.txt 
visudo -c -f sudoers.txt 
sudo cp -X sudoers.txt /etc/sudoers 

完成後,桌面上的sudoers.txt文件可以放入垃圾箱。

要撤消更改,使用命令:

sudo cp /etc/sudoers.original /etc/sudoers 

這是使用測試OS X 10.10.1

下面是每個命令功能的簡要說明:

cd ~/desktop 

這可以確保您使用的是桌面文件夾。

sudo cp -n /etc/sudoers /etc/sudoers.original 

這將備份您的sudoers文件。備份可用於撤消更改。 -n選項確保現有的sudoers.original文件不會被覆蓋。

sudo cp /etc/sudoers sudoers.txt 

將sudoers文件複製到您的桌面。添加.txt擴展名是爲了讓OS X知道這是一個文本文件。

sudo chmod ug+w sudoers.txt 

更改文件的權限以允許寫入權限。

open sudoers.txt 

在TextEdit應用程序中打開該文件。您需要編輯文件並保存更改。

visudo -c -f sudoers.txt 

檢查編輯後的文件是否有語法錯誤。輸出應該是sudoers.txt: parsed OK

sudo cp -X sudoers.txt /etc/sudoers 

將文件複製回/ etc目錄。

+0

我非常感謝您在回覆中的時間和精力,因爲我一直很喜歡學習。然而,我不舒服地將賬戶作爲su(或sudoer)添加到每個與這些權限一起運行的命令中,對嗎?這對我來說似乎是一種安全風險。 – 2015-01-08 17:13:04

+0

@Mike你有沒有想過只給妻子的帳戶權限附加到主機文件?這樣你的腳本就不需要管理員權限。她將無法刪除或修改hosts文件中的內容。她只能附加到它。從終端應用程序輸入命令:'sudo chmod + a「WifesUserName允許追加」/ private/etc/hosts'以允許追加。關閉追加'sudo chmod -a「WifesUserName允許附加」/ private/etc/hosts「。使用'ls -le/private/etc/hosts'查看追加的當前狀態。 – 2015-01-20 09:24:17

+0

@David_A對不起,延遲響應,但我錯過了此評論之前。感謝您提供有趣的替代方法。因爲我更喜歡Alfred的腳本可以便攜式共享,所以這不是我想要的解決方案,但它可以添加到我的未來的心智工具箱中。 – 2015-10-30 15:32:26

0

2015年1月8日編輯

麥克,我理解你的問題:「我有一個do shell script,要求一個管理員密碼一個AppleScript,但我有我之前點擊密碼對話框可以輸入它,我怎樣才能確保對話的重點,省去點擊它的麻煩?「

我對你的迴應是:「如果你的AppleScript從來沒有要求過密碼,你會不會更開心?

如果是,則here's a solution。該問題演示瞭如何通過提供password參數以及with administrator privileges參數來繞過對話。

+0

我不明白這是如何重複的問題。我特別詢問是否有人在使用優勝美地10.10之前的技術方面有經驗,但現在失敗了。我不想編輯sudoers或手動添加用戶登錄密碼到鑰匙串中。我必須錯過一些東西,因爲我認爲這些解決方案在我的方案中沒有任何優勢。 感謝您的迴應。 – 2015-01-08 17:03:40

+0

@MikeM,看我的編輯。 – 2015-01-08 18:29:04

0

我偶然發現了這個帖子,試圖讓密碼對話框獲得我正在處理的Alfred腳本的焦點。在我的shell命令之前加入tell me to activate,我終於可靠地工作了。由此產生的腳本是:

tell me to activate 
do shell script "<command>" with administrator privileges 

希望這適用於你。

+0

我也使用Applescript與阿爾弗雷德,但不幸的是,這並沒有爲我工作。我看到的唯一區別是我在tell塊中使用激活(我或當前的應用程序 - 它們實際上是相同的),並且您在沒有塊的單行中激活。 10.10.1也沒有爲我解決這個問題。 我正在使用一個獨立的腳本文件,而不是阿爾弗雷德內的NSApplescript容器。我更喜歡在腳本編輯器中進行編輯,並且發現嵌入的腳本對於長於幾行的任何東西都是單調乏味的。但獨立腳本通常在我的經驗中受到的沙箱限制更少。 – 2015-01-08 16:52:51

0

好吧,邁克這是我第二次嘗試答案。

相反,你在你上面的問題開始提出使用

tell current application 
    activate 
    set output to (do shell script "printf '" & hostsLine & commentString & "' >> /private/etc/hosts" with administrator privileges) 
end tell 

的,請嘗試使用

tell current application 
    activate 
    tell me to set output to (doShellScript for "printf '" & hostsLine & commentString & "' >> /private/etc/hosts" with administratorPrivileges) 
end tell 

其中doShellScript處理器將

on doShellScript for command as text given administratorPrivileges:adminPriv as boolean : false 
    if adminPriv then 
     set message to (name of current application) & " wants to make changes. Type your password to allow this." 
     set again to "" 
     set pw to "" 
     set icn to 1 
     repeat 
      set fullCommand to "printf '%s\\n' " & (quoted form of pw) & " | sudo -p '' -S " & command 
      try 
       return do shell script fullCommand 
      on error eStr number eNum partial result rList from badObj to expectedType 
       set errorMessage to "Sorry, try again." & return & return & "sudo: 1 incorrect password attempt" 
       if eStr ≠ errorMessage or eNum ≠ 1 then 
        error eStr number eNum partial result rList from badObj to expectedType 
       end if 
      end try 
      set actual to again & message 
      set values to display dialog actual default answer "" with icon icn with hidden answer 
      set again to "Sorry, incorrect passord. Try again." & linefeed 
      set pw to text returned of values 
      set icn to 2 
     end repeat 
    end if 
    return do shell script command 
end doShellScript 

一個例子對話如下所示。

Sample dialog box

您的應用程序的名稱和圖標會有所不同。最主要的是當彈出對話框時,重點將放在密碼文本框中。

dave。

0

我有一個類似的問題,我有一個applescript在終端中執行一個連續的ping,然後會彈出一個對話框讓用戶點擊OK來殺死該ping並啓動一個不同的ping。問題與終端窗口後面顯示的對話框類似。由於我在非常有限的時間內不得不在劇烈的情況下執行這個腳本,這非常煩人。我終於可以通過告訴系統事件在開始持續ping之後等待1秒鐘,然後在打開對話框之前按鍵CMD + Tab切換回正在運行的腳本來解決它。在CMD + Tab鍵最初只是間歇性地解決它,直到我加入了延遲問題1.

例子:

tell application "Terminal" to do script pingCommand 
set userDialog to display dialog "Client Connection Test Completed" buttons {"Ping Server", "Cancel"} 

有了上面的代碼,該對話框總是會在終端窗口的後面。分辨率

例子:

tell application "Terminal" to do script pingCommand 
delay 1 
tell application "System Events" to key code 48 using {command down} 
set userDialog to display dialog "Client Connection Test Completed" buttons {"Ping Server", "Cancel"} 
0

我面臨同樣的問題,並創建了一個解決方案,把我的小程序的對話框到前面。

我寫了一個名爲ActivateMe()的函數,它取代了可以執行「顯示對話框」命令的「激活」或「告訴我激活」命令。

此函數獲取當前進程名稱,然後通過shell腳本執行以下AppleScript命令。

delay 0.1 
    tell application "System Events" 
     set frontmost of process \"" & processName & "\" to true 
    end tell" 

這裏是函數。請享用。

on ActivateMe() 

    set myPath to path to me as text 
    if myPath ends with ":" then 
     set n to -2 
    else 
     set n to -1 
    end if 
    set AppleScript's text item delimiters to ":" 
    set processName to text item n of myPath 
    if (processName contains ".") then 
     set AppleScript's text item delimiters to "." 
     set processName to text 1 thru text item -2 of processName 
    end if 
    set AppleScript's text item delimiters to "" 

    set a to "delay 0.1" 
    set b to "tell application \"System Events\"" 
    set c to "set frontmost of process \"" & processName & "\" to true" 
    set d to "end tell" 
    do shell script "osascript -e '" & a & "' -e '" & b & "' -e '" & c & "' -e '" & d & "'" 

end ActivateMe