Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Nord

macrumors member
Original poster
Apr 28, 2010
80
7
Hello everyone. :)

I've been looking for a way to make my life easier, I have tons of little Automator scripts with max. 2 tasks (move this, rename that, pause itunes, etc.), but now I need something a bit more tricky and hope there are some cracks out there who can help me out, as I have no idea how to do this and suspect it will need a value or some script to achieve. How to move a file into existing folders based on the current date which changes every month? Here's the deal:

When I download a monthly online receipt, I would like Automator to scan its content (format is always PDF) and based on the information (like say the name of the company) to rename the file into: Bill - [compagny] - [month] [year] and move it to an already existing folder in my Documents which is structured as followed:

Main Folder is "2015"
2015 > January 2015
2015 > February 2015
2015 > March 2015
etc…

I was able to manage the first part until the moving-folder-bit.

1.) First problem: As a concrete example (see image below), the company is called "Sanitas". When I download the bill PDF from Sanitas' website, it comes in an indescribable file name like "invoice-195809192.pdf" which makes no sense at all. The Action Folder sees the PDF, scans it for the word "Sanitas" in its content. When found, it changes its name to "Bill - Sanitas - July 2015.pdf" (I found the variable "today's date", so that works fine). The next step is to move it to the folder July 2015. How do I do that? Can a value do this or does it needs a script? Either-way, it must be able to recognize that it needs to move it to July 2015 this month, August 2015 next month, when I download the new bill by the end of August. This is where I stumble.

2.) Also, some bills come "late", which means, it's a bill for the month June but can only download it in July. I need to change the files name but not with "Today's date" but "Last month date". Is there a way to do this?

3.) Lastly, this has more to do with the action folder process itself; sometimes a downloaded file into the "Downloads" folder which the Folder Action points to does not see the newly added file. Is this an error of Folder Action? When I move the file manually say from Dekstop to Downloads, it works. But when the file is saved directly to Downloads from the internet (I'm using Safari), the Folder Script doesn't activate and the file stays there. Is this a bug?

Thanks you so much for your help.
(P.-S. my system is in french, so almost everything's in french too ;-)

Folder+Action+Script.png
 
Your English seems quite good. Help us to help you by posting screen shots in English.

You can do this my creating a new user account, switching to it, setting its primary language to English, and then using Automator while logged into that account.

Creating a new user account:
System Preferences > Users and Groups
Then: click the + button at bottom of left-side list.
Then: fill in the account info.

Fast switching between accounts:
System Preferences > Users and Groups
Then: Login Options (bottom of left-side list)
Then: Checkbox "Show fast user switching menu as..."

Primary Language:
System Preferences > Language and Text
Then: Drag "English" to the top of the list of languages.
Then: Logout and log back in to account.
 
Last edited:
Here's a possible example of using a Run AppleScript action after your first Filtrer les elements du Finder action. It uses the Finder and the creation date property of a file. It should get you going. There's no error checking whatsoever.

Code for the Run AppleScript action :

Code:
on run {input, parameters}
    (* Your script goes here *)
    if input is not {} then
        tell application "Finder"
            repeat with aFile in input
                set creationDate to creation date of aFile
                set creationDateMonth to month of creationDate as string
                set creationDateYear to year of creationDate
                set aFileExt to name extension of aFile
                set newFileName to "Bill" & space & "-" & space & "Sanitas" & space & "-" & space & creationDateMonth & space & creationDateYear & "." & aFileExt
                set name of aFile to newFileName
                set label index of aFile to 2
                move aFile to folder (creationDateMonth & space & creationDateYear) of folder "2015" of folder "PdfFiles" of folder "Downloads" of folder "kryten" of folder "Users" of startup disk
            end repeat
        end tell
        --return newFileName
    end if
end run
 

Attachments

  • Screen Shot 2015-07-29.png
    Screen Shot 2015-07-29.png
    54.7 KB · Views: 623
Yeah, sorry about the screenshot in french… By the way chown33, there's an easier way to switch languages. I already have 3 langues set up in the preferences, one just needs to drag the desired language atop (or drag the top down) and restart the app to have it in the desired langage, like this:
skitch.png

I was just too lazy to change it, my bad. :p Here again, the screeshot in english below.
Thanks for your answer kryten2. I'll try it up an will give you a refresh status. :)
This seems really complicated, I don't understand much, but I'll try my best.

skitch.png
 
Last edited:
I'm back.

Thanks again Kryten2, the script works well, however, all it did was change the file name… o_O
Maybe I was over too complicated in my explanations. The real problem is how do I get automator to move the file automatically to the appropriate, already existing folder, which changes from month to month? So, In july, when the file recognizes the word Sanitas in the PDF, it changes its name and moves it to July 2015. Then in August, the same Automator action script does the name changing and all but then moves the file in the August 2015 folder, and so on, every month.

Simply put: how do I tell Automator to automatically move a file to a preexisting folder but changing the destination path every month?

To give you a better idea of my folder structure, here's how it looks like:
skitch.png

And here's one the path 'till "2015":
~/Bibliothèque/Users/[username]/Documents/Finances et administration/Classeur administratif/2015


Thanks a bunch for your help. :)
 
Strange that you're using such a folder structure inside your user Library folder. I'm guessing you really meant
/Users/[username]/Documents/Finances et administration/Classeur administratif/2015. Your screenshot also shows the name of the folders inside the 2015 folder start with 01 02 etc. You need to change the move statement to the correct folder structure. Based on my guess that would be something like :

Code:
move aFile to folder ((creationDateMonthInt as string) & space & creationDateMonth & space & creationDateYear) of folder "2015" of folder "Classeur administratif" of folder "Finances et administration" of folder "Documents" of folder "username" of folder "Users" of startup disk

Try this for the Run AppleScript action :

Code:
on run {input, parameters}
    (* Your script goes here *)
    if input is not {} then
        tell application "Finder"
            repeat with aFile in input
                try
                    -- insert actions here
                    set creationDate to creation date of aFile
                    set creationDateMonth to month of creationDate as string
                    if month of creationDate as integer is less than 10 then
                        set creationDateMonthInt to "0" & (month of creationDate as integer)
                    else
                        set creationDateMonthInt to month of creationDate as integer
                    end if
                    set creationDateYear to year of creationDate
                    set aFileExt to name extension of aFile
                    set newFileName to "Bill" & space & "-" & space & "Sanitas" & space & "-" & space & creationDateMonth & space & creationDateYear & "." & aFileExt
                    set name of aFile to newFileName
                    set label index of aFile to 2
                    move aFile to folder ((creationDateMonthInt as string) & space & creationDateMonth & space & creationDateYear) of folder "2015" of folder "Classeur administratif" of folder "Finances et administration" of folder "Documents" of folder "username" of folder "Users" of startup disk
                  
                on error the error_message number the error_number
                    display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
                end try
            end repeat
        end tell
        --return newFileName
    end if
end run
 
@enedil, that's interesting, it's quite complicated (for me at least) but if I understand correctly, it has to do with my problem getting a file for a the previous month, am I right? I'm quite tired, I'll have to read this very thoroughly later (after an afternoon sleep), as I don't get most of what's written there :p; (Well… even if I sleep, I don't get much more, but at least I can say I've tried, haha :D). Thanks for the link! Do you think you'd now how to integrate it in such a script like kryten2 did? I'm so frustrated, 'cause I think I'm a very good and experienced OSX user, I know a lot of tricks and most UI functions present in all Apple apps (shortcuts and all), but not a single line of code!! How frustrating is that? When it comes to Automator, I'm in front of my screen like a noob, trying to hopelessly understand the logic, but… :rolleyes:

@kryten2, by the way kryten2, thanks a lot for your effort! My gosh, that's amazing, I hope it's not taking you too much time to do? I had hoped this whole thing would be solved in a line or 2 of code, how ignorant of me. :confused:
Oh yeah, by the was, sorry, it's of course not in the library. As you stated, here's the correct path:
/Users/[username]/Documents/Finances et administration/Classeur administratif/2015
I have numbers in front of each folder, because I have so many folders called "July 20xx", like this I recognize them instantly when searching them with spotlight. ;)

I realize how important each detail is necessary to be correct for script, so I'm sorry if I wasn't precise enough and sometimes even wrong. I think it's getting in the right direction though. There was an error message, it seems that the script is trying to write and search everything in english. The error say: impossible to obtain folder "07 July 2015", etc… since it's in french 07 Juillet 2015:

skitch.jpg

Is it possible to search for the folder in french? Is it the "creationDateMonthInt" command doing this? Because it seems the process itself is correct. If I can get to run it 'till tomorrow, I'll be able to test it as soon as we're passing to August.:)

Thanks all of you for your precious help. Sorry again, I never thought it'd be that complex of a thing…:(

...

P.-S. I'll end up using all smileys at this pace… :cool:
:apple::apple:
 
Try this for the Run AppleScript action :

Code:
on run {input, parameters}
    (* Your script goes here *)
    if input is not {} then
        set month_english_list to {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
        set month_french_list to {"Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"}
       
       
        tell application "Finder"
            repeat with aFile in input
                try
                    -- insert actions here
                    set creationDate to creation date of aFile
                    set creationDateMonth to month of creationDate as string
                    set month_french to item (my list_position(creationDateMonth, month_english_list)) of month_french_list
                    if month of creationDate as integer is less than 10 then
                        set creationDateMonthInt to "0" & (month of creationDate as integer)
                    else
                        set creationDateMonthInt to month of creationDate as integer
                    end if
                    set creationDateYear to year of creationDate
                    set aFileExt to name extension of aFile
                    set newFileName to "Facture" & space & "-" & space & "Sanitas" & space & "-" & space & month_french & space & creationDateYear & "." & aFileExt
                    set name of aFile to newFileName
                    set label index of aFile to 2
                    move aFile to folder ((creationDateMonthInt as string) & space & month_french & space & creationDateYear) of folder "2015" of folder "Classeur administratif" of folder "Finances et administration" of folder "Documents" of folder "username" of folder "Users" of startup disk
                on error the error_message number the error_number
                    display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
                end try
            end repeat
        end tell
        --return newFileName
    end if
end run

on list_position(this_item, this_list)
    repeat with i from 1 to the count of this_list
        if item i of this_list is this_item then return i
    end repeat
    return 0
end list_position
 
  • Like
Reactions: Nord
Amazing! It works perfectly. You're amazing kryten2. :)

I'll wait for tomorrow and try it out to see if it sends it into the folder August and if it does, I'll change this thread as resolved; but already, many, many thanks to you for your help!

I hope this can help other people too which may have had the same idea as me but could not do it (and still can't :p), my script is in french, but the original one is in english. One just needs to change the name of folders and voilà. And you also didn't forget the label red, that's awesome.

Do you have an idea how to do the same thing making it a month earlier? So, when the file is being saved in July, that it actually writes Facture - Sanitas - June 2015.pdf instead of July? All my bills except one are for the current month. This "black sheep bill" always comes late. :D

I'll report back in a few hours.
Again, thank you very much, all of you for your help. ;)
 
Last edited:
Hello again. :)

So apparently, it doesn't move the file to the next month (August). Since the original file was already in the "Juillet" (July) folder, the following error occurred:
skitch.png

It says: 'An item of the same name already exists at this location.'
(I'm translating for others, my guess is you speak french kryten2, coming from Belgium. ;))

I'm sorry, I hope this is not too complicated for you.
I'm just curious, did you learn to script by yourself? Can you recommend a good website of book (french, english or german).
 
Hello again. :)
I'm just curious, did you learn to script by yourself? Can you recommend a good website of book (french, english or german).

Sorry to deviate from the main thread a little. I wonder whether we should have a "Beginners Resources" thread in this forum, particularly for AppleScript.

Either way, my personal story is that I started off (over ten years ago!) merely copying and pasting scripts that nearly did what I wanted and adapting them slightly to meet my needs. I then did a little reading around the subject. At the time I started with AppleScript for Dummies - I expect that's horribly out of date nowadays and it was an irritating read (too jokey) but it covered the bare basics in a way in which I understood them. After that, Matt Neuberg's "Applescript: The Definitive Guide" helped a lot. Again, I don't know how up to date that is these days.

Also very helpful were the forum where you're reading this message, macscripter.net and http://macosxautomation.com/applescript/index.html Last, but certainly not least, Apple's AppleScript mailing list has some useful folks on it.

Good luck!
 
Thanks, I'll be checking out if I can learn a bit myself, 'cause right now, I'm totally clueless.
Any ideas about the changing month problem?

P.-S. Sorry I was out of the loop for some time, I had an emergency and was gone for a few days.
 
Hey everybody.

I just want to add an important update. I've tried kryten2's script on an other file, by changing the name within the script and it worked perfectly! I'm unsure, but I think the error above is due to the created date. I must've used the same file too much and haven't seen my mistake, after trying out again the Sanitas file later, it worked like a charm.

Therefore, problems solved. Thanks a bunch kryten2 for you help! :)

For others info, Post #9 is the one that helped me.

I may come back for another challenge some other time.;)
Thanks again to all participants. :apple:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.