2013-05-28 23 views
1

我正在嘗試製作一個腳本,它將拍攝屏幕截圖,將圖像保存到桌面並將其命名爲日期。類似於如果我使用cmd + shift + 3會發生什麼情況。唯一的問題是圖像的名稱只是「屏幕」而不是我指定的全名。有人知道怎麼修這個東西嗎?以當前時間拍攝屏幕截圖並保存爲桌面名稱

on run 
    set theDesktop to POSIX path of (path to desktop as string) 
    set theCurrentDate to current date 
    set shellCommand to "/usr/sbin/screencapture " & theDesktop & "Screen Shot" & theCurrentDate & ".png" 
    do shell script shellCommand 
end run 
+0

我剛剛做了一個非常相似的腳本。我沒有爲「screencapture」提供完整的路徑名稱,它工作得很好。 –

回答

3

把完整的文件路徑用雙引號,像這樣:

on run 
    set theDesktop to POSIX path of (path to desktop as string) 
    set theCurrentDate to current date 
    set shellCommand to "/usr/sbin/screencapture \"" & theDesktop & "Screen Shot" & theCurrentDate & ".png\"" 
    do shell script shellCommand 
end run 

文件名包含空格,因此,在您的版本,在命令行解釋爲多個參數來/usr/sbin/screencapture

+0

謝謝!我總是很驚訝難題有如此簡單的答案。 –

2

通過上述路徑中的適當的方式是通過使用的引形式:

on run 
    set theDesktop to POSIX path of (path to desktop as string) 
    set theCurrentDate to current date 
    set shellCommand to "/usr/sbin/screencapture " & quoted form of (theDesktop & "Screen Shot" & theCurrentDate & ".png") 
    do shell script shellCommand 
end run 
1

我只是用這樣的外殼命令:

screencapture -i ~/Desktop/$(date +%Y%m%d%H%M%S).png 

-i是交互模式(如⇧⌘ 4)。默認的文件名格式是這樣對我的安裝:

date '+Screen Shot %Y-%m-%d at %-H.%M.%S %p.png' 

man screencaptureman strftime

如果使用AppleScript,則不需要運行處理程序,默認情況下/usr/sbin/位於路徑上,並且可以使用quoted form of轉義參數。

"Screen Shot " & (current date) & ".png" 
do shell script "screencapture ~/Desktop/" & quoted form of result 

如果文件名看起來像在Finder中Screen Shot Wednesday, May 29, 2013 4/47/15 AM.png,這是因爲HFS使用冒號作爲路徑分隔符。在Finder中的:顯示爲/,反之亦然。