Opening an image file using Sikuli/Jython without clicking - python

I'm writing a script that saves an image file to the desktop (so "image.JPG"). After doing that, I want to open the image file so that it is displayed on the screen. I've been trying to use subprocess (which I have no experience with), but can't seem to get it to work.
import subprocess
subprocess.Popen("C:/Users/first.last/Desktop/image.JPG")
However, that results in the following error:
[error] OSError ( Cannot run program "C:/Users/sean.sheehan/Desktop/image.JPG" (in directory "C:\SikuliX"): CreateProcess error=193, %1 is not a valid Win32 application )
I'm assuming this is because it is a file rather than an application (maybe I have to open an application that allows you to view an image?)
So, is there a way to open an image file in sikuli/jython/python without having to double click it with sikuli? I'd also prefer not to download any additional packages.
Thanks.

If it was a normal Python, you would use os.startfile():
Start a file with its associated application.
import os
filename = "C:/Users/first.last/Desktop/image.JPG"
os.startfile(filename)
But, the problem is that the are things (C-based functionality) that exist in Python, but are missing in Jython. And os.startfile() is one of these things.
subprocess.call() should work in Jython:
import subprocess
filename = "C:/Users/first.last/Desktop/image.JPG"
subprocess.call('start ' + filename)
Also see more options at:
Open document with default application in Python

Related

Python script to open a file in Spotfire

So I want to add a button in my text area in Spotfire which will open an excel file (that is connected to my spotfire visualisation) or at least to a network folder with that file.
I believe I can write an ironpython script just to open that file and make changes. How will I do that?
Update:
After some googling I have tried to run a simple script smth like:
t=open('D:/data/folderA/folderB/file.xlsx','w')
To avoid problems with "/" or "\", I also tried importing os
import os
t=open('D:','data', 'folderA', 'folderB', 'file.xlsx', 'w')
Neither of these work.
For those who is still struggling to find the solution, it turned out to be simpler than I thought it would.
from System.Diagnostics import Process
Process.Start(r 'start c:\test\abc.xlsx')

How to gather data from a file and execute a Python file using it via right click?

This title is a bit confusing but let me explain. So say you have a file called test.txt. If you right click on it, it will most likely have a menu appear and will say stuff like "Open in Notepad". How can I get a python file to show in that menu so that it could say "Open in program.py" and the program would run and for example
print(data)
input()
Data being the contents of test.txt. Does anyone know if this is possible?
You need to edit the registry entries in Windows. See this to how.
You have to replace the path with your own script such as C:\\Program Files\\Something\\YourPythonScript %1
%1 represents the associated file name.
Edit: In your python script, you can use sys.argv in order to retrieve the file name of the associated file and process on it.
import sys
#Get selected file
filename = sys.argv[1]
you can use other programs to open your foo.py python code. but that program can't execute the code directly just by one click right. you have to open in program that can run python codes and run the code there.
hope this help you.

Specify Input File in SPSS Statistics

In older versions of SPSS, one could ask for the path to a file using a syntax similar to this:
filename = GetFilePath("*.txt","txt", ,"Specify input file",0)
How can I achieve the same result using recent versions of SPSS, that rely on Python?
I found the answer. So similarly to how the VBA in .SBS script works, Python can use a library directly from SPSS to show a file input dialog.
BEGIN PROGRAM.
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw()
filename = askopenfilename()
print(filename)
END PROGRAM.
sbs/wwd scripts are still supported on Statistics.
As for the prompt, you could create a wwd/sbs dialog to prompt for the file name and then open it, or, without using scripting, you could create a custom dialog box using the Custom Dialog Builder (on Utilities) containing a Browse control and issue a GET command.
If you want this to happen when Statistics is launched, you can create a startup script or use the STATS PROJECT extension command, depending on exactly what you want to do.
I am not aware of SPSS Statistics ever having a function such as GetFilePath, but you can use the Python spssaux.getDatasetInfo function for this, e.g., in a program write
import spssaux
filename = spssaux.getDatasetInfo()

Open file with Adobe Captivate from command line

I am attempting to open an XML file with Adobe Captivate in my script using os.system(). Here is my code:
os.system("open /Applications/Adobe\ Captivate\ 9/Adobe\ Captivate.app/ \"flashcards_template_changed.xml\"")
It works fine the issue is with the opening screen on Adobe Captivate. When the program is run one of those 'New Document' windows pops up and asks you whether you want to start a new document, what kind of document, etc. Similar to Microsoft Word.
I was wondering if anyone had any experience bypassing this menu so the file would open. When I open Adobe Captivate and open my XML file it opens perfectly so I know it is in the right format.
Any help would be great! Thanks!
If Adobe Captivate is registered as the default application for handling XML files, then you can leave out the application name altogether:
os.system("open flashcards_template_changed.xml")
Otherwise, specify the application with the -a flag:
os.system("open -a captivate flashcards_template_changed.xml")
I don't know for certain that "captivate" is a recognized name for Adobe Captivate; I'm just guessing, and I don't have access to a suitable Mac to find out. If that doesn't work, you could probably use something very similar to what you had in your post:
os.system("open -a '/Applications/Adobe Captivate 9/Adobe Captivate.app' flashcards_template_changed.xml")

Opening custom file types in cxFreeze compiled python executable on opening file

I am using cx_Freeze to compile a Rubiks Cube Simulator in python; which uses tkinter.
I would like the user to be able to save the layout of the 2d representation you can see in the centre, into .cube files, and be able to open previous .cube files from the program itself.
However, I also want the user to be able to open .cube files from explorers and have the program startup displaying the contents of the .cube file that the user opened.
Having done some research, I think I need to access the "Runtime Environment" or something - but otherwise I have absolutely no idea.
UPDATE
I solved this using the argparse module. Based on the fact that every time explorer opens a file it calls the application with the argument of the file directory, all I had to do was add an optional parameter to catch this data.
import argparse
parser=argparse.ArgumentParser()
parser.add_argument("cubefile",nargs="?",default=False)
#'nargs="?"' makes the argument optional
#-meaning an error will not be thrown if no file is parsed on execution
args=parser.parse_args()
if args.universefile != False:
init_defaultcube = cubetools.getCubeFromCubeFileDir(args.universefile)
#cubetools is my class and getCubeFromCubeFileDir just interprets the text in the file
However, because this argument changed the working directory of the exe, and my GUI image references were relative, I had to reset the current directory using
os.chdir(os.path.dirname(os.path.abspath(sys.executable)))
I'm now working on modifying the registry on initialisation to set the default app and icon of .cube files.

Categories

Resources