【分享】Applescript 快速縮圖

緣起:
我在Mobile01的相機板看到 笨蛋也會用的縮圖軟體Photo Resize,覺得非常不錯因此在我的 blog 也做了操作的介紹,當時我就想如果在Mac有這樣的縮圖軟體不是很棒嗎?

因此我找到以前這位 robg 寫的 Drag-and-drop script to quickly resize any image ,你只要將Applescript存成應用程式放在桌面,將照片拖放到icon,就會自動將照片縮為寬120pix的圖像更名存回原目錄。
那如果我要像PhotoResize那樣,在程式名稱後面加上數字,如800,就縮成寬800pix 的圖可不可以呢?

實作:
那我先講一下怎樣將樣式比對出來,先講第一種比較笨的方法,然後再講樣式比對的方式

第一種方式:
如果我在桌面建立一個空檔案叫resize600.app,我要怎樣知道這裡面含有600這3個數字?
【分享】Applescript 快速縮圖
我先輸入 ls ~/Desktop/resize*
找出的結果是 /Users/jkchang/Desktop/resize600.app*
然後用 cut 指令ls ~/Desktop/resize*|cut -d . -f 1 將 .app 前面取出來
(將句點分作左右兩個欄位,如果是 cut -d . -f 2 則得到 app*)
結果: /Users/jkchang/Desktop/resize600
然後再用 cut 指令將 / 符號的第5個欄位取出來 ls ~/Desktop/resize*|cut -d . -f 1|cut -d / -f 5
結果: resize600
這時已經將resize600拿到了,剩下就是去掉resize剩600,一樣用cut 將第7個字以後的取出
ls ~/Desktop/resize*|cut -d . -f 1|cut -d / -f 5|cut -b 7-

結果: 600

第二種方式(樣式比對):
【分享】Applescript 快速縮圖
得到結果一樣是600
關於shell的樣式比對可以參考shell的書籍或是你用perl正規式來作也行!

接下來我把程式碼加入Applescript中
【分享】Applescript 快速縮圖

將增加的script 放在 to rescale_and_save(this_item) 後面,取得的數字放入變數 d 中:(--後面當作是註解)
--我增加的功能
--方式一
--set c to do shell script "ls ~/Desktop/resize* | cut -d . -f 1 | cut -d / -f 5 | cut -b 7-"
--方式二
set c to do shell script "a=`ls ~/Desktop/resize*`;b=${a%%.*};c=${b##*/};echo $c|cut -b 7-"
--display dialog c
set d to c as integer
class of d

剛開始你可以將display dialog c前面的--拿掉,在你將照片拖入程式icon時會顯示出多少像素的數字來驗證是否正確,
將Applescript存成應用程式於桌面,檔案名稱叫resize600

【分享】Applescript 快速縮圖
【分享】Applescript 快速縮圖

這時你將照片拖入程式icon時會出現600的框框
【分享】Applescript 快速縮圖

然後將set the target_width to 120 改為 set the target_width to d

還有存檔時原本是在檔案名稱加上"scaled."
tell application "Finder" to set new_item to ¬
(container of this_item as string) & "scaled." & (name of this_item)
save this_image in new_item as typ

我改為檔案名稱前加上600_
tell application "Finder" to set new_item to ¬
(container of this_item as string) & c & "_" & (name of this_item)
save this_image in new_item as typ


執行後的樣子:
【分享】Applescript 快速縮圖

縮圖放回原檔案夾內並在檔案名稱前加上600_
【分享】Applescript 快速縮圖

如此一來你只要修改成想要縮圖的寬度那丟進的檔案就會按你輸入的來進行縮圖。


後記:

1.我這程式目前只能放在桌面,而且當檔案夾名稱是含有中文時會有問題。

2.修改後的Applescript如下,紅色字體部份是修改後的:
~~~~
on open some_items
repeat with this_item in some_items
try
rescale_and_save(this_item)
end try
end repeat
end open

to rescale_and_save(this_item)
--我增加的功能
--set c to do shell script "ls ~/Desktop/resize* | cut -d . -f 1 | cut -d / -f 5 | cut -b 7-"
set c to do shell script "a=`ls ~/Desktop/resize*`;b=${a%%.*};c=${b##*/};echo $c|cut -b 7-"
--display dialog c
set d to c as integer
class of d


tell application "Image Events"
launch
set the target_width to d
-- open the image file
set this_image to open this_item

set typ to this_image's file type

copy dimensions of this_image to {current_width, current_height}
if current_width is greater than current_height then
scale this_image to size target_width
else
-- figure out new height
-- y2 = (y1 * x2) / x1
set the new_height to (current_height * target_width) / current_width
scale this_image to size new_height
end if

tell application "Finder" to set new_item to ¬
(container of this_item as string) & c & "_" & (name of this_item)
save this_image in new_item as typ

end tell
end rescale_and_save


2009-02-21 15:40 發佈
文章關鍵字 applescript



非常感謝啊!

這裡也分享一個解rar的
是利用http://www.rarlab.com/download.htm官方的unrar for mac指令工具去做的
所以要先安裝好他,印象中是把『unrar'檔複製到/usr/bin吧
前半段取得所在資料夾位置 是參考termhere
後面是用unrar自己的指令,加上-kb參數代表既使是下載到一半的rar檔也可以解,很適合載到一半掛掉的影片或圖片壓縮檔。
解壓縮目的地的部份則是參考JK.Chang樣式比對的部份完成的!



把unrarMac.app拖曳到finder的toolbar上放開 (要拿掉的話請按著cmd鍵在把它拉出來)

像這樣

然後之後就可以在finder把你要解壓縮的rar檔拖曳到他上面放開
他就會自動開啟終端機來解壓縮了,會解出一個資料夾:『解_壓縮檔檔名』,然後裡面是解好的檔案。

不知道有沒有辦法把unrar本身也包進app裡啊?
然後applescript能抓得到unrar的位置(如: /Applications/隨便亂丟的某個資料夾/unrarMac.app/Contents/Resources/unrar )

這樣就不用事先安裝unrar了
有人知道嗎
感謝

applescript:

on open theFiles

tell application "Finder"
set thisFolder to (target of front window) as Unicode text
set posixPath to quoted form of POSIX path of thisFolder
end tell

repeat with theItem in theFiles
set itemPath to quoted form of POSIX path of (theItem as string)

tell application "Terminal"
activate
do script "cd " & posixPath & " && unrar x -kb " & itemPath & " ./解_" & c & "/"
end tell

end repeat

end open




##----------------------------------------------------
update
搞定了,只是不曉得這樣重新打包unrar檔案會不會有什麼問題。


on run
display dialog "請將rar檔案拖放到此程式上以解壓縮。"
end run

# theFiles : 取得拖曳進來的檔案清單
on open theFiles

repeat with theItem in theFiles
set itemPath to quoted form of POSIX path of (theItem as string)

tell application "Terminal"
activate
#取得本app所在目錄
set thePath to path to me
set appPosixPath to POSIX path of thePath
#unrar檔案放在本app下的Contents/Resources/資料夾內
set unrarpath to appPosixPath & "Contents/Resources/unrar"


#取得壓縮檔所在目錄
set sourcePath to do shell script "dirname " & itemPath
display dialog "將解壓縮到: " & sourcePath

#取得目標目錄
set targetdir to do shell script "basename -s .rar " & itemPath
set targetdir to "/解_" & targetdir

#cd到壓縮檔所在目錄 #解壓縮
do script "cd " & sourcePath & " && " & unrarpath & " x -kb " & itemPath & " '." & targetdir & "/'"

end tell

end repeat

end open


unrarMac.app.zip:
http://b5.s3.p.quickshareit.com/files/unrarmaczip3f103.zip

JK.Chang wrote:
緣起:我在Mobil...(恕刪)


預覽程式就有這些功能囉 :)
內文搜尋
X
評分
評分
複製連結
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?