2014-04-15 42 views
0

我是蘋果的新手。我想開發一個應用程序applescript用於顯示圖像屬性的applescript代碼

下面是我的代碼。運行它時,它顯示無法生成別名的查找程序。請幫助我。

property script_name : "image Tracker" 
property script_description : "image property data finder" 

--set the_folder to (choose folder with prompt "Select the folder for get the image property:") as Unicode text 

--set the_folder to quoted form of POSIX path of (choose folder with prompt "Select the folder for get the image property:") 

set the_folder to (choose folder) 

copy {"jpg", "png", "eps", "ai", "tif", "psd", "gif"} to validExtentions 

tell application "Finder" to set fPaths to file the_folder 
repeat with thisFilePath in fPaths 
    if name extension of thisFilePath is in validExtensions then 
     try 
      tell application "Image Events" 
       -- start the Image Events application 
       launch 
       -- open the image file 
       set this_image to open this_file 
       -- extract the properties record 
       set the props_rec to the properties of this_image 
       -- purge the open image data 
       close this_image 
       -- extract the property values from the record 
       set the image_info to "" 
       set the image_info to the image_info & ¬ 
        "Name: " & (name of props_rec) & return 
       set the image_info to the image_info & ¬ 
        "*********************************************" & return 
       set the image_info to the image_info & ¬ 
        "File: " & (path of image file of props_rec) & return 
       set the image_info to the image_info & ¬ 
        "File Type: " & (file type of props_rec) & return 
       set the image_info to the image_info & ¬ 
        "Res: " & item 1 of (resolution of props_rec) & return 
       set the image_info to the image_info & ¬ 
        "Color Space: " & (color space of props_rec) & return 
       copy (dimensions of props_rec) to {x, y} 
       set the image_info to the image_info & ¬ 
        "Dimemsions: " & x & ", " & y 
      end tell 
     on error error_message 
      display dialog error_message 
     end try 
    end if 
end repeat 

set myfile to open for access ("Macintosh HD:Users:varghese.pt:Desktop:") & "image_data.txt" with write permission 
write image_info to myfile 
close access myfile 

如果有人找到了答案,請給我的細節。 在此先感謝。

Nidhin約瑟夫

+0

我建議你刪除的問題,您的電子郵件地址;如果有的話,答案將在這裏發佈,網站本身會通知您。 – mklement0

回答

0

讓我說在前面:不幸的是,在一般情況下,AppleScript的是一個挑剔的野獸,有時試錯是你唯一的朋友。

有幾個問題,你的代碼 - 一些明顯的,一個不:

  • validExtentionscopy {"jpg ...聲明中的拼寫錯誤 - 你以後參考用(正確拼寫)名字validExtensions

  • tell application "Finder"命令中,您必須將file替換爲folder

  • 在命令set this_image to open this_file中,您必須將this_file替換爲​​。注意:括號和as text是至關重要的,因爲該文件將通過Finder打開(因此在Preview.app中打開) - 不要問我爲什麼這樣。

  • 我也建議與(path to desktop as text) & "image_data.txt"

替換寫入到文件命令硬編碼路徑如果我們把它放在一起,我們得到:

copy {"jpg", "png", "eps", "ai", "tif", "psd", "gif"} to validExtensions 

set the_folder to (choose folder) 

tell application "Finder" to repeat with thisFilePath in folder the_folder 
    if name extension of thisFilePath is in validExtensions then 
     try 
      tell application "Image Events" 
       # start the Image Events application 
       launch 
       # open the image file 
       # set this_image to open thisFilePath 
       set this_image to open (thisFilePath as text) 
       # extract the properties record 
       set the props_rec to the properties of this_image 
       # purge the open image data 
       close this_image 
       # extract the property values from the record 
       set the image_info to "" 
       set the image_info to the image_info & ¬ 
        "Name: " & (name of props_rec) & return 
       set the image_info to the image_info & ¬ 
        "*********************************************" & return 
       set the image_info to the image_info & ¬ 
        "File: " & (path of image file of props_rec) & return 
       set the image_info to the image_info & ¬ 
        "File Type: " & (file type of props_rec) & return 
       set the image_info to the image_info & ¬ 
        "Res: " & item 1 of (resolution of props_rec) & return 
       set the image_info to the image_info & ¬ 
        "Color Space: " & (color space of props_rec) & return 
       copy (dimensions of props_rec) to {x, y} 
       set the image_info to the image_info & ¬ 
        "Dimensions: " & x & ", " & y 
       # my dlog(image_info) 
      end tell 
     on error error_message 
      display dialog error_message 
     end try 
    end if 
end repeat 

set myfile to open for access (path to desktop as text) & "image_data.txt" with write permission 
write image_info to myfile 
close access myfile 

最後,您可以通過從列舉Finder上下文中的文件切換到System Events上下文中的枚舉來改進代碼的性能

如果您在上面的代碼中將以下兩行替換爲其當前對應行,則腳本將運行得更快。你爲什麼問?再次,我不知道。

  • tell application "System Events" to repeat with thisFilePath in alias (the_folder as text)
  • set this_image to open (get path of thisFilePath)
+0

我使用了這段代碼,但是這個錯誤再次像文件一樣準備打開,寫入文件時圖像信息變量中沒有數據。請幫助我。我對這個應用程序的使用:我需要獲取文件夾中圖像的一些屬性;圖像名稱明智。將所有內容寫入一個文件。而已。 – aadhi

+0

@aadhi:什麼具體會產生錯誤,錯誤消息是什麼> – mklement0