2013-08-28 165 views
0

我經常在學校計算機之間進行切換,並且希望顯示隱藏文件,但不是每個人都這樣做。通常我使用如何獲取Applescript輸入以運行終端命令

"defaults write com.apple.finder AppleShowAllFiles -bool true"

命令,但是,那將是非常方便的,如果我可以運行一些AppleScript的,而不是手動複製文本到終端,然後重做這個時候我完成了。所以我試圖完成的是接收用戶輸入的信息,看他們是否要顯示所有文件,然後運行該命令。對AppleScript進行一些初步研究後,我能夠弄清楚如何構建它的一些基本概念。下面的代碼是錯誤的,所以請以noob錯誤爲藉口。

(choose from list {"Hide", "Show"} ¬ 
    with prompt "Do you want to hide or show hidden files?") 
if "Hide" then 
    do shell script "defaults write com.apple.finder AppleShowAllFiles -bool False" 
else 
    do shell script "defaults write com.apple.finder AppleShowAllFiles -bool True" 
end 

我能站起來給用戶的對話框,但是,當我試着輸入一個選擇,它回答:「到boolean類型‘’不能做」隱藏。如果有人能幫助我,讓我看看我需要改變什麼,那將是不勝感激。

謝謝,邁克爾。

回答

3

choose from list返回所選項目的列表。

choose from list {"Hide", "Show"} with prompt "Do you want to hide or show hidden files?" 
if result is {"Show"} then 
    do shell script "defaults write com.apple.finder AppleShowAllFiles -bool true" 
else 
    do shell script "defaults write com.apple.finder AppleShowAllFiles -bool false" 
end if 
quit application "Finder" 

我用這樣的腳本來切換顯示隱藏文件:

do shell script "x=$(defaults read com.apple.finder AppleShowAllFiles) 
[ $x = 1 ] && b=false || b=true 
defaults write com.apple.finder AppleShowAllFiles -bool $b" 
tell application "Finder" 
    quit 
    delay 0.1 -- without this delay Finder was not made frontmost 
    launch -- open Finder in the background 
    delay 0.1 -- without this delay there was sometimes a "connection is invalid" error 
    activate -- make Finder frontmost 
    reopen -- open a new default window 
end tell 
+0

非常感謝您的急躁迴應,您對我的代碼所做的更改似乎工作得很好。 –

0

從列表中選擇返回一個列表(或者,如果用戶取消假)。這樣做是爲了要挾列表轉換爲字符串:

(choose from list {"Hide", "Show"} ¬ 
    with prompt "Do you want to hide or show hidden files?") as string 

請務必保持括號,否則,將as string強迫你的提示字符串,你仍然會收到一個列表返回。