2014-02-08 47 views
0

iPhoto表示不得不重建其數據庫,之後以前的手動排序事件都搞砸了。我可以以編程方式批量編輯iPhoto事件標題嗎?

我試着從備份恢復舊數據庫,但iPhoto會重新生成,再次搞亂了它。

能夠從iPhoto圖庫備份內的AlbumData.xml文件中提取事件的原始順序(編輯此文件不會修復訂單,悲哀地)。

我找不到用AppleScript或其他方式改變手動事件順序的方法(也許你可以用SQLite以某種方式做到這一點)。

但編輯iPhoto事件標題,添加數字前綴(「1.我的第一個事件」,「2.我的第二個事件」),然後按標題而不是手動排序事件似乎更可行。這似乎也不太脆弱。新事件可以被命名爲「1.1我的中等事件」。

但是爲了達到這個目的,如果我可以以編程方式批量編輯標題,這將很方便。那可能嗎?我在AppleScript Dictionary中看不到任何有用的內容。

回答

0

我開始對它們進行手動編號,然後我意識到iPhoto具有漂亮的腳本鍵盤快捷鍵:編輯一個事件標題時,您可以選擇下一個。所以我編寫了這個腳本。

這是紅寶石(我的首選武器)和AppleScript的一個非常醜陋和hackish的組合,它的寫入解決我的具體問題,但它可能給你如何能做到這一點是一個好主意:

def osascript(cmd) 
    system "osascript", "-e", cmd 
end 

def assign(number, name) 
    `echo | pbcopy` # clear out clipboard in case the title is empty and the cmd+c fails 
    osascript %{tell app "System Events" to keystroke "c" using command down} # copy 
    actual_name = `pbpaste` 
    name_was_empty = actual_name.to_s.chop.empty? # names generated from dates may show as empty when you try to edit them 

    if (actual_name == name) || name_was_empty 
    puts "go on" 
    osascript %{tell app "System Events" to keystroke (ASCII character 28)} # left 
    osascript %{tell app "System Events" to keystroke "#{number}. "} 
    osascript %{tell app "System Events" to keystroke #{name.inspect}} if name_was_empty 
    osascript %{tell app "System Events" to keystroke tab} 
    else 
    puts "Got: #{actual_name.inspect} but expected #{name.inspect} (number: #{number})" 
    osascript %{tell app "iPhoto" to display dialog "Not the name I expected. Bailing!"} 
    exit 1 
    end 
end 

osascript %{tell app "iPhoto" to activate} 
osascript %{tell app "iPhoto" to display dialog "Click the first event title and wait…"} 
sleep 3 

# You would probably loop over some data structure here. 
assign(1, "My first event") 
assign(2, "My second event") 
# … 

Also available as a Gist.