omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


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

    Pythonista
    safari
    2
    4
    4205
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • NeroDanza
      NeroDanza last edited by

      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()```
      1 Reply Last reply Reply Quote 0
      • cvp
        cvp last edited by 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))
        
        
        1 Reply Last reply Reply Quote 1
        • NeroDanza
          NeroDanza last edited by

          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?

          1 Reply Last reply Reply Quote 0
          • cvp
            cvp last edited by

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

            1 Reply Last reply Reply Quote 1
            • First post
              Last post
            Powered by NodeBB Forums | Contributors