How do I call out to a command-line program from a gnome shell extension? - gnome-shell

I have written a simple shell script to accomplish a common task, and I want to be able to run it whenever a button is clicked. I've used gnome-shell-extension-tool to create the Hello World example already, but now I need to know how to simply have it run an arbitrary command when clicked. There is no input or output to be concerned with; it just needs to run.

After some more creative googling, I've found the solution:
const Util = imports.misc.util;
Util.spawn(['/path/to/program', 'arg1', 'arg2'])

const GLib = imports.gi.GLib;
let stuff = GLib.spawn_command_line_sync("cat hello.txt")[1].toString();
For those looking to read the output of the command, use this.
The default working directory for Gnome shell extensions is the user's home directory.
Just thought I'd mention these things, because it took me a while to figure them out.

Related

Using iMacros for Firefox to run an external exe?

I want to run an iMacros .js script from the free Firefox version to open an executable file (via the command line).
I want to put this script on a loop, so this has to be done repeatedly.
I have looked online and it looks like the free version does not allow this directly. I am looking for a workaround. Perhaps using a bat file and a scheduler? I am not sure about it.
It looks like it should be simple enough. Is there anyone out there who has done something like this before?
Hamza
This is possible in firefox with this .js command:
var file = Components.classes["#mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("C:\\Users\\Administrator\\Desktop\\app.exe");
file.launch();
You can also launch any app or file of your choice by this.

how do I run my .py file from within python command line?

background info:
I know this question has been asked countless times, but I'm not understanding the answers.
Today is the first time I've ever done anything with python, so I'm a total noob.
I'm using windows 7.
python 3.3.5 lives here: C:\Python33\python.exe
I have a .py file I want to run saved here: C:\Users\Scydmarc\Documents\cs188\python_basics\myfile.py
If I simply double click on myfile.py, it opens, runs, and closes super fast. By doing a perfectly timed printscreen, I can see that it works. It is my understanding that I need to run the file from within python.exe to get the window to stay open and allow me to mess with variables etc after it runs. So I open python.exe. What exactly do I need to type to run myfile.py? I've found lots of people trying to do this, but I guess I'm not understanding the answers. When I try to follow along, I get tangled up with errors. Do you still need to put python before the file name while in the python.exe window? Do I somehow need to specify a full path to the file? Some are actually running from the windows command prompt and not the python command prompt. When trying to change PYTHONPATH, are you supposed to do that from inside python, or the windows command prompt? Some examples use '/', while some use '\'. So, I think a complete example (what to actually type, in what window, line by line, with real filepaths) would clarify a lot for me and be super helpful. I'm thinking if I can actually see it done once, I can figure out how to make it do what I want in the future. Thanks for any help you can give.
in your python dir type python.exe pathtofile.py or set python.exe to your classpath
edit: https://www.youtube.com/watch?v=IokKz-LZsEo

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.

How do I save my command snippets for quick access from the commandline?

I have a huge file with all the commands I use on and off the commandline. This file is getting harder to open and navigate as its size gets larger. I am looking for a commandline utility that makes this process easier. I found this gem, which does something similar to what I want, but it's terrible with long lines of code with multiple quotation marks.
What does everyone use to keep your code snippets and easily access it from your terminal?
I keep a similar file myself. This example isn't directly useful with your file. However when I find that I need to run a particular command I recently used, without looking it up again, I run a grep on history.
For example:
history | grep "svn co"
You may try Komandi, a multiplatform command snippets manager.

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