How can I set an AppleScript to run when a given file is modified? - applescript

I know that applescripts can be set to run as folder actions, but is there a way to get them to run as file actions? Yes, I know I can work around this by just making a new folder, putting the script in it, and running as a folder action, but I want to know if this can be done.

I cross-posted this question to Apple's Applescript discussion board, and a few users there gave some solutions that use only system resources, with no third-party applications. Follow the link to see the answers.
Basically you need to use launchd to watch a file and launch a script when the file changes. Then you'd need to craft a .plist that describes the script to run and the file(s) or directories to watch.

There's no simple built in way to do this. However there's lots of ways to set up an Applescript to run. However in general you'll have to do some non-Applescript coding.
Check out the info in this hint and OSXHints:
http://hints.macworld.com/article.php?story=20060817044149264
They have a Python script demonstrating using fsevents.so to monitor file changes. You would then just modify the Python to call your Applescript (or simply use Appscript to do the scripting from within Python).
Alternatively you can just have a program check a file at a particular interval and then run if the file changes. This hint describes that in Perl:
http://hints.macworld.com/article.php?story=20031001073403810

I found a way to do this that works.
Shell script:
find watchedfilepath -mtime -1 -exec cp {} watchedfolderpath ';'
Create a Calendar Event in Automator to run this shell script which will check the file. If it has been modified in the last 24 hours, then it will be copied to the watched folder. Then create an action in Automator to move it to trash (otherwise the next time the shell script runs it won't work).
Set up a Folder Action on the watched folder to watch for an added new item and then it will let you know. I have created a script to send me an email and put that in the Folder Actions folder in Library/Scripts. It works like a treat.

This is an old question, but the Apple Automator tool in MacOS does exactly this. You can set it to watch a folder and execute an Applescript, Shell Script, etc.

Related

Trying to Set a Shortcut to a Directory

Im trying to run several different scripts on my Mac for data stored in different paths. So I put all said scripts into a folder. In order for a script to work, I have to be cd'd to a specific folder required for said script. I use the scripts to get different types of output (ex: Image volumes, MRI info, etc..).
I was wondering if there was a way to store all my scripts in one directory and assign a shortcut to it. That way, I can cd to my folder, and only type something like $ScriptsFolder/Script_1 to have it. Ive seen this done in the FSL software package, where typing $FSLDIR/data/standard would give you the items in that folder.
UPDATE:
Im using Script=/Users/ray/Documents/Script which works until I close down my terminal to start a new one. Any way to make the setup permanent?
EDITED
Found this helpful in the end.
http://www.techradar.com/how-to/computing/apple/terminal-101-creating-aliases-for-commands-1305638
I used it to create a shortcut to my scripts which are all placed in a folder for easy referencing.

monitor a file on mac os x

There are a lot of similar posts out there, but none that does exactly what I want: I want a simple script, that watches a folder on a Mac for changes and passes the filename of the modified file to command/script when changes are detected.
The tool inotifywait doesn't exist on a mac. Folder events or fswatch or watchdog seem to be getting close, but it turns out they only watch folders or only return the folder name. watchdog might be doing what I need, but I could only find commands like this
watchmedo shell-command --recursive --command='echo ${watch_src_path}' .
which again only passes the folder name in the variable watch_src_path. Does anyone know how to get it (or another program) to pass on the file name of the changed file?
Even if the question is very old, it may be useful to other people looking for the same functionality. I wrote fsw exactly to fill this gap. fsw will give you the name of the changed paths and the type of change event.
Hope this helps.
Edited: fsw was merged with fswatch.

How to make a script auto install under Ubuntu

I have pretty big assignment where I had to create some files for word frequency checks and a bash script connected with a crontab to make these function run under a specific set of folder every 5 minutes.
The last step here is to somehow compress these folders and scripts so anyone can easily install it on another computer, so some sort of auto installer for these scripts.
I'm not really sure what I should use, if a tarball can achieve this or if I need to use some sort of packet manager to be able to complete this step.
I would be really thankful if someone can explain the best way to complete this, and preferably some links to some documentation about this.
Thank you.
--
Just to clarify,
I need this to some how create the directories and subdirectories like it's looking now, and preferably have something that adds the function automaticly to the crontab record.
The proper way to do that is a .deb package. You would install the cron in /etc/cron.d/
You could also use a shar and add the commands you need to that. Or use tar, let the user extract it in a temp directory then tell them to run an installer. In that installer (which you would write) use either ordinary cp commands, or the install command.

Windows: File Monitoring Script (Batch/VBS)

I'm currently in working on a script to create a custom backup script, the only piece I'm missing is a file monitor. I need some form of a script that will monitor a folder for file changes, and then run a command with the file that's changed.
So, for example, if the file changes, it'll execute "c:/syncbatch.bat %Location_Of_File%"
In VBScript, you can monitor a folder for file changes by subscribing to the WMI __InstanceModificationEvent event. These articles contain sample code that you can learn from and adapt to your specific needs:
WMI and File System Monitoring
How Can I Monitor for Different Types of Events With Just One Script?
Calling WMI is fairly cryptic and it causes the WMI service to start running which can contribute to bloat since its fairly large and you really can't cancel the file change notifications you've requested from it without rebooting. Some people experimenting with remote printing from a Dropbox folder found that a simple VBScript program that ran an endless loop with a 10 second WScript.Sleep call in the loop used far less resource. Of course to stop it you have to task kill that script or program into it some exit trigger it can find like a specifically named empty file in the watch folder, but that is still easier to do than messing with WMI.
The Folder Spy http://venussoftcorporation.blogspot.com/2010/05/thefolderspy.html
is a free lightweight DOT.NET based file/folder watching GUI application I'ved used before to run scripts based on file changes. It looks like the new version can pass the event filename to the launched command. The old version I had didn't yet support file event info so when launched, my script had to instance a File System Object and scan the watched folder to locate the new files based on criteria like datestamps and sizes.
This newer version appears to let you pass the file name to the script if you say myscript.vbs "*f" on the optional script call entry. Quotes can be important when passing file paths that have spaces in folder names. Just remember if you are watching change events you will get a lot of them as a file grows or is edited, usually you just want notification of file adds or deletes.
Another trick your script can do is put the file size in a variable, sleep for a few seconds, and check the file again to see if its changed. if it hasn't changed in a few seconds you can usually assume whatever created it is done writing it to disk. if it keeps changing just loop until its stable.

MacOSX open file with an application: where does it go to?

I posted this basic question before, but didn't get an answer I could work with.
I've been writing applications on my Mac, and have been physically making them into .app bundles
(i.e., making the directories and plist files by hand). But when I open a file in the application by right clicking on the file in finder and specifying my app, how do I then reference that file?
I mostly use python, but I'm looking for a way that is fairly universal.
My first guess was as an argument, as were the answers to my previous post, but that is not the case.
Py:
>>> print(sys.argv[1:])
'-psn_0_#######'
Where is the file reference?
Thanks in advance,
The file is passed by the Apple Event, see this Apple document. You need to receive that from inside your Python script. If it's a PyObjC script, there should be a standard way to translate what's explained in that Apple document in Objective-C to Python.
If your script is not a GUI app, but if you just want to pass a file to a Python script by clicking it, the easiest way would be to use Automator. There's an action called "Run Shell Script", to which you can specify the interpreter and the code. You can choose whether you receive the file names via stdin or the arguments. Automator packages the script into the app for you.
This is not an answer but it wouldn't fit in the comments. To respond to #Sacrilicious and to give everyone else insight on this:
#Sacrilicious You're talking about something different. Download this sample application, it's a python script wrapped as an "App". Look inside and find a 4-line python script: myscript.app/Contents/MacOS/myscript - which will print the arguments using
file = open("/tmp/test.txt", "w")
file.writelines(sys.argv[1:])
Stick it in your Applications folder. Then right click some file and choose "Open With" and select this myscript.app.
Now take a look at /tmp/text.txt and you'll see that something like -psn_0_####### is there and not the name of the file you had selected "open with". This is because the file is passed using Apple Events and not a filename as an argument.
So this question is asking how can you access the filename of the thing that was passed in the python script wrapped in an OS X .app application wrapper, and if someone can let me know that they'll get the Bounty :)
Are we referring to the file where per-user binding of file types/extensions are set to point to certain applications?
~/Library/Preferences/com.apple.LaunchServices.plist
The framework is launchservices, which had received a good amount of scrutiny due to 'murkiness' early in 10.6, and (like all property list files) can be altered via the bridges to ObjectiveC made for Python and Ruby. Here's a link with Python code examples for how to associate a given file type with an app.
I've never heard of it being done without a Cocoa / Carbon wrapper.
I described how to link certain filetypes to py2app-bundled Python applications at https://moosystems.com/articles/8-double-click-on-files-in-finder-to-open-them-in-your-python-and-tk-application.html

Resources