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

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

Related

How to Hide the source code in xcode?

I am writing an app using applescriptobjc in xcode, and while making the archive I see the source code is still inside the package, is there a way to hide it or transform it completely to binary code?
Thanks
I may be a year late to the discussion but I asked the same question around the same time as you did last year and hope that the answer I got will satisfy your own question. Here is an extract answer from
my own question:
When you save the AppleScript file you have the option of checking "Run Only" - in that case the script is compiled into a new form that can be run exactly as the original script, but it is not meant for further editing/development.
It doesn't mean that the run-only file is complete protected or non-readable, but parts of your code, comments, etc. are not included or not easily readable.
If you want to do more you'll need a code obfuscator if you still want to use AppleScript - however I'm not sure if one exists on the market, or you need to develop your own.
As you can see, a simple way to do this is:
Open your final AppDelegate.applescript file in Script Editor on the mac.
Select: File > Export...
2.1. Don't change the file name but override the archive file
2.2. BEFORE SAVING: Choose Run-only checkbox from the save options but leave the File Format as Script
Now your file cannot be opened in plain text as it is set to Run-only. Please remember that it is not a 100% read-proof solution, but works great for small projects (provided you are not rushing the final export and don't forget to perform the above mentioned steps before handing it over to the customer).
I hope you can make the most of this answer and still deem it useful. Let me know how you get on or whether you have any more questions. Perhaps you have since found a far better solution (or just an alternative), if so, I am still looking for the next best thing in source code protection.
In Xcode 9.2. (and maybe earlier versions) you can find under Build Settings the item OSACompile and can choose to compile to an executable only directly from Xcode.

Passing parameters/values from Emacs to external application

I'm working on a plugin for a very niche CAD application that our company uses. The development environment is entirely based on Emacs and everything should be done through it.
In this case, code should be edited, navigated, compiled and debugged using Emacs. There is no separation from Emacs and the actual language compiler (from what we know so far - there is no compiler.exe).
Since my team is not a fan of Emacs we are trying to get rid of this dependency so we can use whatever editor we want an compile/interact with the application without Emacs, but looks like this is very hard because everything is built inside it, even go-to definition, find all references all and sorts of commands.
These are all defined in hundreds of .el files (LISP) that are called using custom commands from Emacs.
However, when calling "Run this file" for example, said file is ran inside the application and correctly executed so my question is: what mechanisms could be used used for this to happen? I don't expect precise answers as I can't share the name of the actual CAD application or anything else, but it would be really helpful to understand what options are possible so I can investigate them.
In summary, whenever I "Run this file" the actual application is started by Emacs by calling the command line and the actual file gets executed inside the application. Looks like the contents of the Emacs buffer are passed to the application somehow. This "how" is what I'm trying to find.
Thanks for any help.

How do I make an executable that will add my Automator service which runs a bash script?

My friends are losing points in their computer science classes because their submission graders decompress and read zip files, which are obfuscated by a bunch of Apple metadata crap generated from zipping a file through the right-click contextual menu in Finder.
I've written a bash script to zip only unhidden files from a specific directory, and I've added a contextual menu service through Automator so this script can be run from right clicking a file.
The problem is I don't want to manually copy over all this stuff and make Automations on each computer my friends use. I'd like to leverage make or some similar utility to make an executable that will add the bash file and automator menu service to any computer the executable is opened from.
I'm lightly familiar with make (more familiar with rake), but I can't find out how to package the automator service and install it on their machine through an executable.
You'll want to package your automator application and script(s) into an Installer Package (.pkg). The best way to achieve this is to understand some general concepts, the commands to build one, and by checking out some tutorials or other relevant information.
I recommend for creating your first Installer Package using a utility
such as Packages (open-source and free), which has excellent
documentation, and is less complicated than using the command
line tools. Basically it puts a nice gui around the Apple tools which
are somewhat nonintutive by themselves.
Building Installer Packages is not the easiest thing to do your first time, and it might take a bit of patience to finally get it right. Once you've successfully created one you should have a good grasp on what is involved in putting them together along with the concept behind it all. At the very least, this information hopefully points you in the right direction regarding your question, and/or gives you a better idea of how to accomplish what you want to do.

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

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.

Ruby accessible File Manager

I'd like to make a customized file manager using Ruby, but I don't want to reinvent too many wheels. Is there an existing file manager with an open API that could be accessed from Ruby? Or, is there a toolkit or framework available to make my own file manager?
What do you mean by a file manager? Command line or GUI based? Do you need it to be full featured, or are you just trying to do a file selection window, or something like that?
If you'd like a GUI based one - check your OS. There's probably hooks for manipulating the OS's GUI file manager. They may not be offered natively in Ruby, but more than likely they're available in C. You can wrap the hooks in a ruby extension, and then do what you want.
If you'd like a command line like one - check out FileUtils. It gives you the hooks you need to find out the directory contents, move, copy, or rename files, and pretty much anything else you want to do.

Resources