AppleScript to Move and Rename Files

Good day all,


I need help creating an Applescript that appends files with the current date, then moves them to another folder. I also need it to run at a specific time. In Lion, I had a script to just delete these reports at specific time that operated through iCal's open file function in an appointment, but once I upgraded to Mountain Lion it ceased to work and would just pop open the AppleScript editor window and do nothing. Ideally though, I would like to have the reports have the date added and moved, and still at a specific time.


Thanks in advance for your help,


Joshua

OS X Mountain Lion (10.8.2)

Posted on Dec 25, 2012 4:45 AM

Reply
9 replies

Dec 25, 2012 6:15 AM in response to JNJ816

the script is easy enough:


set targetFile to "/path/to/target file"

set destinationFolder to "/path/to/destination folder"


set fp to open for accesstargetFile with write permission

writereturn & return & short date string of (current date) tofpstarting ateof

closefp


tell application "System Events"


movefiletargetFiletofolderdestinationFolder

end tell


but it looks like Calendar has blocked running executables from alerts. you have two options then:


  1. use Calendar to send yourself an email and use a rule action to run the applescript
  2. use a launchd job to run the script on a schedule


The first is straightforward, if circuitous. the second needs a plist like the following stored in ~/Library/LaunchAgents (edit in the path to the above script, save it as plain text, name the file user.script_runner.by_date.plist; script runs daily at 6pm (18:00))


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

<key>Label</key>

<string>user.script_runner.by_date</string>

<key>ProgramArguments</key>

<array>

<string>osascript</string>

<string>/path/to/script/</string>

</array>

<key>StartCalendarInterval</key>

<dict>

<key>Hour</key>

<integer>18</integer>

<key>Minute</key>

<integer>0</integer>

</dict>

</dict>

</plist>

Dec 25, 2012 7:23 AM in response to twtwtw

Thanks for the help. When I try to enter the information into AppleScript editor and run it, I get the following error:


error "Network file permission error." number -5000 from "Macintosh HD/Users/joshuajones/Documents/Company Files/Reports/chief_report.pdf" to «class fsrf»


I'm not really sure what it means or where to go with it.


Thanks again.

Dec 25, 2012 5:12 PM in response to twtwtw

I changed the path as you indicated, but now I get this error:


error "File file Macintosh HD:Users:joshuajones:Documents:Company Files:Reports:chief_report.pdf is already open." number -49 from file "Macintosh HD:Users:joshuajones:Documents:Company Files:Reports:chief_report.pdf"


I am not sure what could have the file open as Acrobat is closed.

Dec 25, 2012 6:23 PM in response to JNJ816

Ah, I just noticed something. you're trying to write a date stamp to the end of a pdf file? That's not going to work, at least not in this simplistic format. Applescript can only deal with plain text files directly.


Could you please describe your workflow more carefully? Everything from begining to end, noting where you get the pdf files, where they go in the long run, and what you're trying to accomplish with it all.

Dec 26, 2012 7:10 AM in response to twtwtw

Sure, no problem. The pdf's are generated from FileMaker, uploaded directly to the server once they are generated via Automator and Fetch, then retrieved whenever needed from my website. It is simpler for me to leave the file with the name minus the date because it's easier for us to retrieve it that way, old reports are automatically overwritten on the server so there are never multiples (which also eliminates housecleaning therein), and to just have the files moved on my computer from one folder to another. Retaining the reports allows me to go back to historical anaylsis, metrics, compare thoughts from whatever timeframe, etc.


For now, I've just been moving them, manually, to the Old Reports folder and allowing the system to just add a 1, then 2, etc. to the files. But ideally, I'd like to have the dates add automatically. Just to take a redundant task off my hands so that I can focus on more important matters.


Perhaps it's Automator I should be trying to use for this?

Dec 26, 2012 10:17 AM in response to JNJ816

The main reason I asked was to find out whether it was possible to add the date first and then convert to pdf, which would be a bit easier. seems not, so the alternate approach I suggest is to create a new pdf with the date (and any other relevant info) and merge it into the pdf at need. you do that using code like the following:


set dateString to short date string of (current date)


-- write the date to a scratch file

set fp to open for access "/tmp/scratch_file.txt" with write permission

writedateStringtofp

close accessfp


-- convert the scratch file to pdf format using CUPS

do shell script "/usr/sbin/cupsfilter /tmp/scratch_file.txt > /tmp/scratch_file.pdf"


-- use automator to combine the two pdf files.

-- note that the return after 'scratch_file.pdf' is necessary; it's a file list delimiter.

do shell script "automator -i '/tmp/scratch_file.pdf

/path/to/fileInQuestion.pdf' '/path/to/muddler.workflow'"


-- paragraph 2 should be a quoted version of the path to the newly-constructed file.

set combinedFilePath to paragraph 2 of the result


sorry if this looks like magic (or like I'm pulling it out of my you-know-what). but it should work.

Dec 26, 2012 2:28 PM in response to twtwtw

Lol, thanks a lot. I really appreciate it. I was able to go back through to Automator instead of AppleScript and was able to configure an application that moved the files and renamed them with the creation date added to the end of the file. I setup the event in iCal and tested it a few times. Worked like a charm. And without the dialogue box popping up as it had been.


But thank you twtwtw for all your help with this. I really do appreciate it!

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript to Move and Rename Files

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.