Forum Archive

How to open a tab in Safari, when already in Safari?

NeroDanza

I have a script here that runs google image search on whatever URL is in clipboard.
It works perfectly **in Pythonista. **

But I want it to work from within Safari (That means from Safari->'Run Pythonista Script'->'My script.py'.

It will not open the tab if run in this way from Safari. Is there a way to make this work?

My code is simply:
```
import clipboard
import webbrowser

def is_url(image_url):
''' Accepts a url [string]. Returns True if url is valid. '''
try:
return image_url.startswith('http://')
except:
print 'Invalid URL.'
return False

def create_url(image_url):
''' Accepts an image_url [string]. Returns search by image URL. '''
search_url = 'https://www.google.co.uk/searchbyimage?&image_url='
try:
search_url += image_url
except:
print 'Could not create URL from clipboard.'
return search_url

def main():
image_url = clipboard.get()
if is_url(image_url):
search_url = create_url(image_url)
webbrowser.open('safari-' + search_url)
print 'Done.'

if name == 'main':
main()```

cvp

When you run Pythonista in the sharing extension, some functions are not available, like webbrowser...
see here

But you can use:

from objc_util import nsurl,UIApplication

app = UIApplication.sharedApplication()
URL = 'https://www.google.co.uk/searchbyimage?&image_url='
app.openURL_(nsurl(URL))

NeroDanza

Thank you. It works very well!

How would I know how to do this without your post though? Should I be reading the objective C iOS api from Apple, or something?

cvp

I had found my-self, months ago, by asking on this forum...
It contains a lot of useful informations from some guru's