简体   繁体   English

AppleScript / Automator文件夹操作将Excel转换为CSV

[英]AppleScript/Automator Folder Action to Convert Excel to CSV

I'm running into a "Grant Access" problem with Office 2016 where I have to manually click "Grant Access" if I'm opening a new file with Automator. 我遇到了Office 2016的“授予访问权限”问题,如果要使用Automator打开新文件,则必须手动单击“授予访问权限”。 I found this answer for how to get around it (by using a file object or an alias object instead of a string): 我找到了解决该问题的答案(通过使用文件对象或别名对象而不是字符串):

tell application "Microsoft Excel"
activate
open file "Macintosh HD:Users:path:to:file"
end tell

But since I'm using an Automator folder action, I'm not sure how to get that file path where it needs to be. 但是由于我使用的是Automator文件夹操作,所以我不确定如何在所需的位置获取该文件路径。 Most of the examples I found has the AppleScript use choose folder with prompt but since the whole point of this is to be fully automated, that's not going to work. 我发现的大多数示例都有choose folder with prompt的AppleScript使用choose folder with prompt但由于此操作的全部目的是完全自动化的,因此无法正常工作。

The idea is: 这个想法是:

  1. Excel file gets downloaded into "ForSQL" folder Excel文件下载到“ ForSQL”文件夹中
  2. Folder action prompts xls file to convert into csv 文件夹操作提示xls文件转换为csv
  3. csv then opens in TextWrangle to ensure it stays in UTF-8 然后,csv在TextWrangle中打开以确保其停留在UTF-8中
  4. Then moves it to official "SQL" folder 然后将其移至正式的“ SQL”文件夹
  5. Closes all the applications it opened and deletes whatever it moved from the "ForSQL" folder 关闭它打开的所有应用程序,并删除它从“ ForSQL”文件夹中移走的所有内容

But I'm open to better suggestions that get to the same end result. 但我乐于接受达到相同最终结果的更好建议。

This is my Automator workflow so far -- but it looks like I need to replace the 'Convert Format of Excel Files' step with AppleScript to get the "Grant Access" pop-up to go away. 到目前为止,这是我的Automator工作流程-但看起来我需要用AppleScript替换“转换Excel文件的格式”步骤,以使“授权访问”弹出窗口消失。 It's a folder action that starts when something hits the "ForSQL" folder: 这是一个文件夹操作,当某些内容击中“ ForSQL”文件夹时开始:

自动化工作流程

I am not sure to understand what you want to do with textWrangle, but the script bellow does all steps before and after, only using Applescript (no need for Automator actions) : 我不确定要使用textWrangle做什么,但是下面的脚本仅使用Applescript即可执行之前和之后的所有步骤(无需执行Automator动作):

--this choose file must be replaced by your "input" of automator folder items 
set Fxl to choose file --the Excel file to be processed


-- define here your destination SQL folder
-- for my tests, I used a folder mySQL on my Desktop
set SQLFolder to ((path to desktop folder) as string) & "mySQL"

tell application "Finder" to set ForSQL to (container of Fxl) as string

--define new name by replacing current extension (xls, xlsx, xlsm, xml)  by "csv"
tell application "Finder"
set N to name of Fxl
set NbExt to length of ((name extension of Fxl) as string)
set newname to (text 1 thru -(NbExt + 1) of N) & "csv"
end tell

--convert to CSV and close
tell application "Microsoft Excel"
open Fxl
tell workbook 1
    tell sheet 1 to save in (ForSQL & newname) as CSV file format
    close without saving
end tell
end tell

-- add eventually your TextWrangle step here (?)

-- delete source file and move csv to proper folder
tell application "Finder"
delete Fxl
move file (ForSQL & newname) to folder SQLFolder
end tell

In practice you can run all this in full applescript folder action. 实际上,您可以在完整的applescript文件夹操作中运行所有这些操作。 Replace the "set Fxl to choose..." by "on adding folder items...". 用“添加文件夹项目...”代替“将Fxl设置为选择...”。 In this case you must insert the script above in a repeat loop to process all files drops in the folder. 在这种情况下,您必须在重复循环中插入上面的脚本,以处理该文件夹中的所有文件。

However, you may have some issues using folder action script, because system will trigger your script again at the time you are creating the new CSV file (again in the same folder !). 但是,使用文件夹操作脚本可能会遇到一些问题,因为在创建新CSV文件时(同样在同一文件夹中!),系统会再次触发脚本。

The work around I suggest is to use folder action script to get the file dropped in ForSQL, move it to an other "temp_ForSQL" and run this script using the moved file. 我建议的解决方法是使用文件夹操作脚本将文件拖放到ForSQL中,将其移动到另一个“ temp_ForSQL”,然后使用移动的文件运行此脚本。 Doing so, the cvs will be added to temp_forSLQ, without triggering new folder action script. 这样做,会将cvs添加到temp_forSLQ,而不会触发新的文件夹操作脚本。

I hope it is clear enough...if not, just let me know. 我希望它已经足够清楚了……如果没有,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM