PatriceK
PatriceK

Reputation: 37

Using automator and AppleScript to move a file to a different folder and subfolder based on name and content of a file

I'm a total newbie on Automator and Scripting... I have read a lot of answers to problems a bit similar to mine, but I don't succeed to adapt with Automator + AppleScript.

Here is what I want to do:

Verification of the file

Right now, I succeeded to obtain year and month in 2 variables, but I can't find a good way to move the file using variables in the next step in Automator.

Automator with 2 variables and problem to move file

Upvotes: 0

Views: 1007

Answers (2)

PatriceK
PatriceK

Reputation: 37

I've simplified the process and found a way with AppleScript:

on run {input, parameters}
    set theFile to input as text
    set yearName to ((characters 34 thru -1 of theFile) as string) --trim first 35
    set yearName to ((characters 1 thru -22 of yearName) as string) --trim last 23
    set monthName to ((characters 39 thru -1 of theFile) as string)
    set monthName to ((characters 1 thru -19 of monthName) as string)
    set destinationFolder to ("Macboot :Travail en cours:Google Drive:Company:Comptabilité:" & yearName & ":" & monthName & ":Compte Transferwise 1:Relevé PDF + fichier CSV:" as text)
    tell application "Finder"
        activate
        move theFile to destinationFolder -- use "copy source_file to folder (target_folder as alias)" to copy the files
    end tell
    set result to {yearName, monthName, theFile, destinationFolder}
    return result
end run

Upvotes: 0

Ted Wrigley
Ted Wrigley

Reputation: 3174

The following should work, or at least get you on the correct path. Set the following for variables:

  • monthName: a text variable that holds the month value, as above
  • yearName: a text variable that holds the year value, as above
  • filePath: a storage variable that holds reference to the file you are working on
  • outputFolder: a storage variable that will hold a reference to the output folder

enter image description here

The first two actions collect the month and year from storage and pass it to the AppleScript action as a list in the input variable. That AppleScript action extracts the values from the list, concatenates them into a path string, and the uses the POSIX File command to turn that into a file reference. The fourth action stores the file reference in the outputFolder variable.

Note that the fifth action ignores the input from the fourth action. Instead, it recovers the original file specifier (that you will have stored somewhere earlier in the workflow, then sends the file specifier to the Move Finder Items actions, which uses the value stored in the outputFolder variable as its destination.

Upvotes: 2

Related Questions