2013-06-25 50 views
0

我試圖自動化不完全腳本化的JPEGmini - 控制它的唯一方法是通過「打開」對話框。Applescript:在文件>打開窗口中獲取項目的絕對路徑

每個圖像反覆調用一次是非常緩慢的。

相反,我試圖在「打開」對話框中執行.jpg或.jpeg文件的搜索,以便在該結果集中選擇我要處理的一批文件並在單程。

(代碼示例後的更多細節)

-- later within the search results list I get, I want to select these 
set filesToSelect to {"/Users/me/Desktop/photo.jpg", "/Users/me/Documents/image.jpeg"} 

-- the actual app is JPEGmini but the same principle applies to Preview 
tell application "Preview" 

    -- start the app 
    activate 

    -- let it boot up 
    delay 3 

    -- ensure it still has focus 
    activate 

end tell 

tell application "System Events" 

    tell process "Preview" 
    -- spawn "Open" window 
    keystroke "o" using {command down} 
    delay 1 
    -- spawn "Go to" window 
    keystroke "g" using {command down, shift down} 
    delay 1 
    -- Go to root of hard drive 
    keystroke "/" 
    keystroke return 
    delay 1 
    -- Perform search 
    keystroke "f" using {command down} 
    delay 1 
    keystroke ".jpg OR .jpeg" 
    keystroke return 
    end tell 

end tell 

做完上面的,我該如何訪問搜索結果列表,重複的每個項目,並獲得它的絕對路徑?

我已經嘗試了一些變化,但我無處可去。

謝謝你的時間。

回答

1

您可以通過在shell命令行和applescript之間使用交叉來簡化此操作。

set jpegSearch to paragraphs of (do shell script "mdfind -onlyin/'kMDItemContentType == \"public.jpeg\" '") 

mdfind命令

諮詢中央元數據存儲,並返回匹配給定的元數據查詢該文件的列表 。該查詢可以是字符串 或查詢表達式。

AFAIK這是聚光燈使用的。

mdfind文件將給你一個這個命令如何工作的想法。但部份腳本只搜索在「/」並查找內容類型的屬性,它是public.jpeg

回報是文本。匹配文件的POSIX路徑列表。這段文字就像一個文本文檔,每條路線都在一條新的線路上,但實際上有一個條目與Applescript相關。

他們需要在蘋果列表中分解。所以我通過詢問結果的段落來做到這一點。

此外,如果你想打開一個新的Finder窗口:

嘗試類似:

tell application "Finder" 
    activate 

    set myWindow to make new Finder window to startup disk 
end tell 

要回答爲什麼你得到你的錯誤在試圖獲得的目標的POSIX路徑文件。

如果您查看搜索窗口中返回的文件之一的屬性。

tell application "Finder" to set fileList to properties of first file of front window 

你會看到該文件的屬性有一個名爲原始項目

如果你真的想這樣做,你正在做它,其中一個方法方式屬性得到原始項目。將結果強制轉換爲別名形式,然後獲取posix路徑。

tell application "Finder" to set aFile to POSIX path of (original item of first file of front window as alias) 

在正常的查找器窗口中,您可以使用。

tell application "Finder" to set fileList to POSIX path of (first file of front window as alias) 

這些只是示例來告訴你怎麼回事。

查找程序窗口結果類型的差異是因爲在搜索窗口中顯示的是原始文件的別名文件(類:別名文件),因此是原始項目屬性。

更新2

要經過您的項目在你的清單,檢查他們對另一名單很簡單。

蘋果有一些工具可以幫助您處理代碼。

在您的腳本中。

crtl +鼠標點擊將保存jpg結果作爲列表的變量。

這會給你一個包含幫手代碼的上下文菜單。 轉到菜單中的重複例程文件夾。

然後到它的 '處理每一個項目'

enter image description here

這將重複例行添加到您的代碼。

enter image description here

並從那裏你可以用它來檢查每個項目根據您的其他名單。

set yourOtherList to {"/Library/Desktop Pictures/Abstract/Abstract 2.jpg", "/Library/Desktop Pictures/Abstract/Abstract 1.jpg"} 

    set jpegSearch to paragraphs of (do shell script "mdfind -onlyin/'kMDItemContentType == \"public.jpeg\" '") 
    repeat with i from 1 to number of items in jpegSearch 
     set this_item to item i of jpegSearch 
     if this_item is in yourOtherList then 

      -- do something 
      log this_item 
     end if 
    end repeat 

重複例程的工作原理是這樣的。

它在給定列表

每個項目將會從第1項到迭代列表中的項目數量遍歷。

i變量保存循環迭代次數。即在第一個循環中它將是1,在第300個循環中將是300。

等第一循環this_item項我jpegSearch就相當於設置爲寫設置this_item第1項jpegSearch

,但蘋果可以節省您不必編寫每個項目的每個數字與重複我 ..

變量可以是您選擇符合正常允許變量命名語法的任何字或字母。

你可以做的事情是從匹配的項目中建立一個新的列表。通過將它們複製到先前聲明的列表中。然後,您可以在重複循環完成後在該列表上工作。

這裏bigList被聲明爲空列表{}

匹配的項目將被複制到bigList。它收到的每個新項目都被添加到列表的末尾。

set bigList to {} 

    set yourOtherList to {"/Library/Desktop Pictures/Abstract/Abstract 2.jpg", "/Library/Desktop Pictures/Abstract/Abstract 1.jpg"} 
    set jpegSearch to paragraphs of (do shell script "mdfind -onlyin/'kMDItemContentType == \"public.jpeg\" '") 
    repeat with i from 1 to number of items in jpegSearch 
     set this_item to item i of jpegSearch 
     if this_item is in yourOtherList then 

      copy this_item to end of bigList 
     end if 
    end repeat 

    bigList 
+0

非常感謝Mark,我可以從中獲得一些提示。 –

+0

我已更新我的問題以更好地解釋我的意圖。這不是我最終感興趣的路徑列表,只是它是我所處階段我需要的下一個東西。道歉。 –

+0

更新我的答案。 – markhunte

相關問題