I would love a script that automatically places any .dll file I download onto my server via SSH. Is this possible?
I'm sure there are several ways to get this done. Although, IMO the task is quite complex for your first script.
To track down the necessary steps:
1.) Watch a source folder for changes
2.) If the added file has a file extension .dll, run step 3.)
3.) Open ssh session
4.) Copy the file from source folder to destination folder
5.) Close ssh session
It's probably easier to make a new 'Folder Action' in Automator and add a shell script than writing an AppleScript.
To give you a basic idea of how it works, create two folders like ~/Documents/TEST/Automator/SourceFolder and ~/Documents/TEST/Automator/DestinationFolder in your home folder.
- In Automator choose from menu > File > New -> select 'Folder Action'
- Set 'Folder Action receives files and folders added to' > Other... > ~/Documents/TEST/Automator/SourceFolder
- Drag'n'Drop from Actions > Library > Utilities > Run Shell Script
- Select 'as arguments' from 'Pass input' -> that will write a loop for you
- Exchange the line 'echo "$f"' with whatever you want to execute (steps 2 to 5)
- Choose from menu File > Save... and give it a name like 'FolderActionCopyFile' -> That saves the folder action to ~/Library/Workflows/Applications/Folder Actions/FolderActionCopyFile.workflow and configures the SourceFolder with that action. By selecting the SourceFolder and right click, you can choose 'Folder Action Setup...' from the context menu and see how your folder is configured or make some changes.
To give a simple working example that you can place inside the 'Run Shell Script' window:
Code:
for f in "$@"
do
cp -np "$f" "$HOME/Documents/TEST/Automator/DestinationFolder/"
done
Every file you'd then place into the chosen SourceFolder will get copied to the chosen DestinationFolder.
Now it's up to you, learning how to implement your exact needs.
E.g., there is the if-statement that you'll want to just process .dll files. Extracting file extensions is done with string manipulations. Then you need to solve the connection to the server. I don't recommend the use of plain text passwords. There are ways to authenticate without entering a password, but by generating and placing authentication keys. Finally, you'll need to decide whether you better use the command cp, mv or rsync (that features copying to remote shell connections as a one-liner) to get the file transfer done.
If you're struggling with the script, asking the folks here on MR at the Mac Programming forum is probably a good idea. Happy Learning Mac OS X Automation...