简体   繁体   中英

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. 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. 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.

The idea is:

  1. Excel file gets downloaded into "ForSQL" folder
  2. Folder action prompts xls file to convert into csv
  3. csv then opens in TextWrangle to ensure it stays in UTF-8
  4. Then moves it to official "SQL" folder
  5. Closes all the applications it opened and deletes whatever it moved from the "ForSQL" folder

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. It's a folder action that starts when something hits the "ForSQL" folder:

自动化工作流程

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) :

--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. Replace the "set Fxl to choose..." by "on adding folder items...". 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 !).

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. Doing so, the cvs will be added to temp_forSLQ, without triggering new folder action script.

I hope it is clear enough...if not, just let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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