2016-09-28 50 views
1

我目前處於創建工作流程的最後階段,該工作流程涉及使用Excel中的數據插入圖像的非常繁瑣且每週繪製的過程。Applescript - 將圖像插入Excel時保留圖像尺寸

我已經設法制定了Automator和Applescript的組合來實現一個工作流程,該工作流程可以在excel中找到圖像,但在我當前的applescript中,它們以設定的大小進入,100 x 100

這裏是問題:

我需要所有的圖像在100px的高度進來,但我需要的寬度要靈活。我似乎無法弄清楚這一點,我認爲這將是一個用於Excel的Applescript命令來鎖定寬高比。

這是當前的AppleScript我使用的實際圖像導入:

tell application "Finder" 
    set imageFolder to "Macintosh HD:Users:adlife:Desktop:Temporary_Image_Hold - Copyright Filings" 
end tell 

tell application "Finder" 
    set imageList to files of folder imageFolder 
end tell 

tell application "Microsoft Excel" 
    set imageHeight to 100 
    set imageWidth to 100 
    set imageCount to count of imageList 
    set theRange to active cell of worksheet 1 of workbook 1 
    set thisStep to 0 
    set theLeft to left position of active cell 

repeat with imageFile in imageList 

    set theTop to (top of active cell) 

    set newPic to make new picture at beginning of worksheet 1 of workbook 1 with properties {file name:(imageFile as text), height:imageHeight, width:imageWidth, top:theTop, left position:theLeft, placement:placement move} 


    set thisStep to thisStep + 1 
end repeat 
end tell 

我在埃爾卡皮坦

運行的Microsoft Excel 2011在這裏的任何幫助將不勝感激!

謝謝!

+0

所以,我只是想出了一個解決方法... 在將圖像插入到我的excel之前,我設置了一個蘋果腳本來運行Photoshop動作,它打開圖像,調整它的大小然後將統一的填充添加到圖像的兩側。這樣,插入到excel中的所有jpegs都是相同的大小 - 只是它們中的一些在邊上有空白區域,有些則不會影響excel數據,所以我們很好走! 我仍然有興趣學習這純粹與AppleScript,但如果你們中的任何一個知道如何! –

回答

0

沒有圖像屬性來強制寬高比。用戶界面中的複選框用於在用戶移動或調整圖像大小時鎖定定量。

解決的方法是獲取圖像尺寸並將比率(寬度/高度)應用於您定義的圖像高度(= 100)。尺寸使用「圖像事件」說明提供。然後,您可以使用新寬度作爲「製作新圖片」中的「寬度」屬性。

set imageFolder to "Macintosh HD:Users:adlife:Desktop:Temporary_Image_Hold - Copyright Filings"as alias 

tell application "Finder" to set imageList to files of folder imageFolder 

tell application "Microsoft Excel" 
set imageHeight to 100 
set imageWidth to 100 -- just a default value, not really needed 
set imageCount to count of imageList 
set theRange to active cell of worksheet 1 of workbook 1 
set thisStep to 0 
set theLeft to left position of active cell 

repeat with imageFile in imageList 

    set theTop to (top of active cell) 
    tell application "Image Events" -- open the image and get dimensions 
     set theFile to imageFile as alias 
     set myImage to open theFile -- don't worry, it will not open the fiel for user, only for the script ! 
     set {W, H} to dimensions of myImage 
     set imageWidth to imageHeight * W/H -- set width using the proportion of image = W/H 
    end tell 
    set newPic to make new picture at beginning of worksheet 1 of workbook 1 with properties {file name:(imageFile as text), height:imageHeight, width:imageWidth, top:theTop, left position:theLeft, placement:placement move} 

    set thisStep to thisStep + 1 
end repeat --next image to place 
end tell 

我測試的腳本(當然是用我自己的圖像文件夾!),並將其與厄爾尼諾Capitain和Excel 2011年

正如你所看到的作品,我也改變了你的代碼的第一線:

要設置imageFolder,您不需要在tell「Finder」塊中執行此操作。但是,它必須設置爲「別名」。

要獲取所有文件,可以單行告訴「Finder」,而不告訴/結束告訴塊。 (更簡單)