2011-08-04 65 views
1

我寫了一個腳本,將PSD作爲PNG與rb-appscript一起導出。這很好,很花哨,但我似乎無法在py-appscript中使用它。如何使用py-appscript將PSD作爲PNG導出?

這裏的Ruby代碼:

#!/usr/bin/env ruby 

require 'rubygems' 
require 'optparse' 
require 'appscript' 

ps = Appscript.app('Adobe Photoshop CS5.app') 
finder = Appscript.app("Finder.app") 

path = "/Users/nbaker/Desktop/" 
ps.activate 
ps.open(MacTypes::Alias.path("#{path}guy.psd")) 

layerSets = ps.current_document.layer_sets.get 

# iterate through all layers and hide them 
ps.current_document.layers.get.each do |layer| 
    layer.visible.set false 
end 

layerSets.each do |layerSet| 
    layerSet.visible.set false 
end 

# iterate through all layerSets, make them visible, and create a PNG for them 
layerSets.each do |layerSet| 
    name = layerSet.name.get 
    layerSet.visible.set true 
    ps.current_document.get.export(:in => "#{path}#{name}.png", :as => :save_for_web, 
          :with_options => {:web_format => :PNG, :png_eight => false}) 
    layerSet.visible.set false 
end 

而這裏的不等價顯然Python代碼:

from appscript import * 
from mactypes import * 

# fire up photoshop 
ps = app("Adobe Photoshop CS5.app") 
ps.activate() 

# open the file for editing 
path = "/Users/nbaker/Desktop/" 
f = Alias(path + "guy.psd") 
ps.open(f) 

layerSets = ps.current_document.layer_sets() 

# iterate through all layers and hide them 
for layer in ps.current_document.layers(): 
    layer.visible.set(False) 

#... and layerSets 
for layerSet in layerSets: 
    layerSet.visible.set(False) 

# iterate through all layerSets, make them visible, and create a PNG for them 
for layerSet in layerSets: 
    name = layerSet.name() 
    layerSet.Visible = True 
    ps.current_document.get().export(in_=Alias(path + name + ".png"), as_=k.save_for_web, 
       with_options={"web_format":k.PNG, "png_eight":False}) 

的Python腳本,不工作的唯一部分是儲蓄。我已經得到各種錯誤嘗試不同的出口選項和東西:

連接無效...發生一般的Photoshop錯誤。此功能可能無法在此版本的Photoshop中使用...無法將某些數據轉換爲預期類型...

我只能使用ruby腳本,但所有其他腳本都在python中,所以能夠在python中實現這一點很好。在此先感謝互聯網。

回答

1

伯特,

我想我有你的解決方案 - 儘管幾個月後。請注意,我是Python noob,所以這絕不是優雅。

當我試用你的腳本時,它也一直在崩潰。然而,通過在出口調用中刪除「別名」,這似乎是確定:

from appscript import * 
from mactypes import * 

# fire up photoshop 
ps = app("Adobe Photoshop CS5.1.app") 
ps.activate() 

# open the file for editing 
path = "/Users/ian/Desktop/" 
f = Alias(path + "Screen Shot 2011-10-13 at 11.48.51 AM.psd") 
ps.open(f) 

layerSets = ps.current_document.layer_sets() 

# no need to iterate 
ps.current_document.layers.visible.set(False) 
ps.current_document.layer_sets.visible.set(False) 

# iterate through all layerSets, make them visible, and create a PNG for them 
for l in layerSets: 
    name = l.name() 
    # print l.name() 
    l.visible.set(True) 
    # make its layers visible too 
    l.layers.visible.set(True) 
    # f = open(path + name + ".png", "w").close() 

    ps.current_document.get().export(in_=(path + name + ".png"), as_=k.save_for_web, 
       with_options={"web_format":k.PNG, "png_eight":False}) 

我注意到,那就是,你通過所有層循環切換自己的知名度。實際上,您可以一次性發布所有命令 - 我的理解是速度更快,因爲它不必繼續發佈Apple事件。

我的腳本的邏輯不正確(關於打開和關閉圖層),但你明白了。

Ian

相關問題