Opening and using Safari - python

I am relatively new to the mac world. My question concerns opening an application using python on mac osx. From what I've found so far, it seems as if applications are stored in app format that are actually directories. Are these parsed somehow by the OS when opening the app? I would like to open Safari using python and it is in my /Applications/Safari.app directory. Is there a specific binary I should be passing to os.system or should I be going about it in a completely different way? My end goal is to have safari open a local html file, close it then open another local html file.
Thanks,
-John

The Python standard library includes the webbrowser module which allows you to open a new browser window or tab in a platform-independent way. It does support Safari on OS X if it is the user's default:
>>> import webbrowser
>>> webbrowser.open("http://stackoverflow.com")
But webbrowser does not support closing a browser window. For that level of control, you are best off using Safari's Apple Event scripting interface by installing py-appscript.
>>> from appscript import *
>>> safari = app("Safari")
>>> safari.make(new=k.document,with_properties={k.URL:"http://stackoverflow.com"})
>>> safari.windows.first.current_tab.close()
If you just want to change the web page displayed in the tab you opened:
>>> safari.windows.first.current_tab.URL.set("http://www.google.com")
>>> safari.windows.first.current_tab.URL.set("http://www.python.com")
Safari's Apple Events interface is somewhat non-intuitive (unfortunately, that's not unusual with Mac applications). There are references out there if you need to do more complex stuff. But Python and py-appscript give you a solid base to work from.

os.system("open -a /Applications/Safari.app http://www.google.com")
for this to work when safari is not the default add -a after open.
Cant comment yet (reputation below 50 (: )

This works for me:
os.system("open /Applications/Safari.app http://www.google.com")

Related

Is it possible to open links in a Python (3.10) terminal?

If I had a print() statement, for example:
print('https://www.google.com')
Is there a way to make a link clickable? And would the same apply for a file path, like this:
print('path/to/file')
What I need is a way to print a link/path to a terminal and then to be able to open it by clicking on it. Is this possible? If yes, how?
Python itself has nothing to do with this behavior- its functionality provided by your terminal emulator.
However, you can open a link programmatically in the browser within python using the builtin webbrowser module, for instance:
from webbrowser import open_new_tab
open_new_tab("https://www.google.com")
In some scenarios, this may be sufficient.

Using python to input text to a different window

I wanted to make a python 3 program that types something in another window, such as Google Chrome.
Is this possible, and if so, how?
This question is a little open ended, but it sounds like you want an http server. You can use either SimpleHTTPServer or make your own from scratch. A basic HelloWorld for using python3's http module is here. I used this simple guide to help me understand the basics of web servers in python. Hope this helps!
import pyautogui
searchbar = (x, y)
text_or_url = "hello word!"
pyautogui.click(searchbar)
pyautogui.typewrite(text_or_url)
pyautogui.typewrite([Enter])
Use pywinauto lib
It is very easy.
For example you have opened notepad window and opened 'Save as' dialog and want to type filename:
from pywinauto import application
app = application.Application()
app.connect(title='test1.txt - Notepad')
app.SaveAs.Edit1.type_keys("C:\\test2.txt")
Use pywinauto lib
filePath.set_text('D:\\aa.txt')
inpuMsg.type_keys('D:\\aa.txt')
set_text
type_keys

webbrowser python open new url in same tab. Does this ever work?

It seems to me that Python's webbrowser 'new=0' functionality (see here), which opens a new url in the same tab or window, is not ever working.
The documentation uses dodgy language like "if possible" to mask this problem.
Has anyone seen any success with this functionality in the webbrowser module? Are there any known workarounds to achieve this functionality?
I have tried setting the webbrowser with
webbrowser.get(TYPE)
before continuing with opening urls. I have also tried using a slew of different browsers, however still have seen no success. Is this just not possible anymore? Should I just use selenium?
Among others, I have checked out this post from 7 years ago. I am hoping things have changed since then and people have found a way around this.
Any help or insight is much appreciated.
I'm using MacOS and Chrome and ran into this. I noticed that for MacOS, webbrowser.py just builds a little applescript and opens the URL with the 'open location' command, e.g.,
script = 'open location "%s"' % url.replace('"', '%22')
The built-in 'open location' command doesn't appear to support opening a url in an existing tab
I searched around in applescript examples and found that you can use commands from the Chrome applescript dictionary to open URL in the active tab like so:
tell application "Google Chrome"
tell front window
set URL of active tab to "www.google.com"
end tell
end tell
I only needed to do this on a local project, so I just went with the dirty fix of overwriting the string in the 'script' variable to force it to use the latter format, like so:
# script = 'open location "%s"' % url.replace('"', '%22')
script = '''tell application "Google Chrome"
tell front window
set URL of active tab to "%s"
end tell
end tell ''' % url.replace('"', '%22')

Automating Flash Program with Python using Pywinauto

I am trying to send a couple basic text commands to a flash program running in Firefox on Windows 7, but I am unable to get pywinauto working for me.
Right now, I have just been able to accomplish the very basic task of connecting to Firefox plugin-container by directing it to the path using the following code:
from pywinauto import application
app = application.Application()
app.connect_(path = r"c:\Program Files (x86)\Mozilla Firefox\plugin-container.exe")
The next step seems to be something to the effect of:
app.plugin-container.Edit.TypeKeys('Text')
However, I can't reference the plugin-container window using '.plugin-container', or any combination of those words. I have tried adding a title variable to the connect_() function and I have tried everything I can think of to find out how to type the command.
The example I am basing this off of is the notepad sample:
from pywinauto import application
app.start_(ur"notepad.exe")
app.Notepad.Edit.TypeKeys(u"{END}{ENTER}SendText d\xf6\xe9s "
u"s\xfcpp\xf4rt \xe0cce\xf1ted characters!!!", with_spaces = True)
It doesn't matter to me if I use pywinauto or Firefox. If it is any easier to do this using a different module or Internet Explorer, I'm on board for whatever accomplishes the task. I am using Python version 2.7.2 and would prefer it over any version changes.
Any help at all is appreciated. I am pretty lost in all this.
As the author of pywinauto - I think you are going to have a hard time. pywinauto only really helps with standard windows controls, and I don't think that flash controls are implemented as standard windows controls (Buttons, Edit boxes, etc).
OFf the top of my head - I would think Sikuli may be a better starting point (http://sikuli.org/).
Another option may be 'http://code.google.com/p/flash-selenium/' - I just googled for "automating flash input" - and it turned up in one of the first articles I clicked.
Thanks for trying pywinauto - I just don't think it is best suited for Flash automation.

Opening links in external browser in Amarok 1.4

I've tried asking this question on the KDE development forum, but haven't received a satisfying answer so far.
I've developed a Python script for Amarok 1.4 which retrieves upcoming events for the currently playing artist and displays them in the context browser. The user can click each event to know more about it, but so far clicking takes him to another Amarok tab, in which he must then click another button to finally get the link to open in the external browser.
What I'd like to know is whether there is a way to open the link directly in the external browser.
Update: I recently started using Pana, which actually opens the links directly in the Wikipedia tab, so I guess I'll stick to that player in the future.
I know nothing about Amarok, but in general you can spawn the platform's default web browser on a URL:
on modern open-source desktops (KDE, GNOME, Xfce) by spawning the xdg-open command;
on OS X with the open command;
on Windows with the built-in os.startfile method.
There is also the webbrowser module, but it will often pick the wrong browser and everything about it is outmoded and ugly.

Categories