Automator or Applescript, detect a new file in folder - file

I am trying to write something that will detect a new file in a certain specified directory. I preferably would like this script to continue running indefinitely, and whenever I see a new file, I can copy it and move it somewhere else. I know this has to be possible, because dropbox does it, but I just do not know how to get this working or where to start. Any ideas?

Here is another example of a folder action. You should save the script in ~/Library/Scripts/Folder Action Scripts .
on adding folder items to theFolder after receiving theFiles
-- This should make the folder action wait until large files have finished copying to the folder
set fSizes to {0}
repeat
tell application "System Events" to set end of fSizes to size of theFolder
if (item -1 of fSizes) = (item -2 of fSizes) then exit repeat
delay 1
end repeat
-- If you want to do something with each file ...
repeat with aFile in theFiles
-- your code goes here
end repeat
end adding folder items to
Keep in mind that if your script saves anything to the target folder, the folder action will be triggered again.

What you're looking for (to spawn off Applescripts when adding new files into a "hot folder") is called "Folder Actions", and there are a number of tutorials that can be found online that should point you in the right direction.
For example, here is one: "AppleScript: Exploring the power of Folder Actions, part I"

Related

Simple Move Files in AppleScript

I poked around and did find code that works but I can't figure out why my simple version didn't work and the more complicated did:
This code does not work, results in error
"Can’t get entire contents of "Macintosh HD:Users:Andrew:Downloads"."
tell application "Finder"
move entire contents of DownloadsFolderAddress to StatementFolderAddress
end tell
But this code DOES work??
tell application "Finder"
set AllStatementsList to get every item of (entire contents of folder DownloadsFolderAddress)
move AllStatementsList to StatementFolderAddress
end tell
Very curious why the second example works and not the first
The error is clear: You cannot get the contents of a literal string.
The second example works because you specified a folder.
Sometimes the Finder can infer the type but it's good practice to specify it always.
Assuming StatementFolderAddress is also the HFS path to a folder
tell application "Finder"
move entire contents of folder DownloadsFolderAddress to folder StatementFolderAddress
end tell

Shake: automatically deleting file after failed command

Using Shake, to create an mp3 (this is just a learning example), I use lame, and then id3v2 to tag it.
If the lame succeeds, but the id3v2 fails, then I'm left with the mp3 file in place; but of course it is "wrong". I was looking for an option to automatically delete target files if a producing command errors, but I can't find anything. I can do this manually by checking the exit code and using removeFiles, or by building in a temporary directory and moving as the last step; but this seems like a common-enough requirement (make does this by default), so I wonder if there's a function or simple technique that I'm just not seeing.
The reason Make does this by default is that if Make has a partial incomplete file on disk, it considers the task to have run successfully and be up to date, which breaks everything. In contrast, Shake records that a task ran successfully in a separate file (.shake.database), so it knows that your mp3 file isn't complete, and will rebuild it next time.
While Shake doesn't need you to delete the file, you might still want to so as to avoid confusing users. You can do that with actionOnException, something like:
let generateMp3 = do cmd "lame" ... ; cmd "id3v2" ...
let deleteMp3 = removeFile "foo.mp3"
actionOnException generateMp3 deleteMp3

Word default folder is different when started programmatically

We have a 2 step process that collects all filenames from a folder into a Word document for use elsewhere.
The original process was to run an old DOS batchfile that collected the filenames into a DOS .txt. Then we launched a Word .docx with a macro that imported the .txt and massaged the formatting. After visual inspection we hit ‘Save’ and that was it.
I had the bright idea that a step could be taken out by launching Word directly from the batch, so I inserted the line: start winword filename. This works great except that the default location that Word wants to save in is now my Templates folder. Running it the old way still works perfectly.
The question is: why is the default location changed by launching Word programmatically and how can it be forced back to the correct location?
Thanks
you can use:
start /D C:\path\to\folder "" winword.exe
this program starts winword.exe and save all files to C:\path\to\folder
ill assume that winword.exe is in the current directory.
for help with the start command : http://ss64.com/nt/start.htm
I investigated the start command, but never did figure out why it operated differently. The eventual solution was to include the Save action in the macro. I still don't know why we didn't have to do that before, but it works now so we're declaring success and moving on.

If a specific folder exists then delete all the contents, else stop the script

I need a script to find if a folder "c:\backup" really exists, delete all the folders contents BUT not delete the "backup" folder itself.
Need to be some "IF" function for security reasons to don´t get the risk for the script don´t find the folder (for any reason) and then start to delete all other folders in the system. So if the script don´t find the folder, the script has to abort the action.
hope it works... this is some VB code that may help you
Dim dir As New IO.DirectoryInfo("path")
If dir.Exists Then
Kill("path\*.*")
Else
' Do whatever else
End If

Connect Direct Multiple files, one Node.

I'm working on a project that requires sending multiple files to the same node. The files are available for sending at the same time and I created a simple C.D shell script to send the files. I looped the call to this script to send all the files (about 20) at the same time.
In my script I'm intending to delete the files within the loop and after the CD script is called. However,.. some one at work , a colleague, told me that the files may not be sent on the spot but rather put in a queue for transmission at a later stage if the C.D node is busy and hence deleting the files would cause errors.
Can someone advise if this is the case? Are the files not fiscally copied even if put in a queue?
I find it weird that the CD script would complete with a successful return code and give me the process number and I still cannot delete the file?
Thanks,
Sergio
You can use the if statement for each file, if the exit code for the file is 0, only the file is deleted and CD moves to the next step for copying the next file.
if (step01=0) then
run task (Do something)
sysopts="rm filename"

Resources