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.


    Interacting with content displayed in current Safari tab

    Pythonista
    5
    8
    7579
    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.
    • gaijinsan
      gaijinsan last edited by gaijinsan

      Hello. I'm totally new to Pythonista 3 and a bit confused as to what is and is not possible with respect to getting it to interact with the currently open app on my iPad and interacting with other apps.

      Some basic scenarios/questions:
      (Note: I use iCab mobile as my browser, but to keep things standard I will ask these with respect to using Safari)

      1. With Safari running and a web page displayed, can I: run a Pythonista script which selects all the text on the web page and copies and pastes it into a new note in the Notes app? How?

      2. With Safari running, can I: run a script which invokes Safari to save the current web page in a particular folders inside my Safari bookmarks? How?

      3. With Safari running, can I: run a script which reads the currently display webpage, looks for tabular data, processes it, and displays it in CSV format in a new Safari tab?

      4. With Safari running, can I: run a script which looks for a string of text in the current web page, if it finds it, copies it to the clipboard, opens Evernote, looks for a pre-existing note with a particular name, and appends the copied text to the bottom of that note?

      Any info on the above capabilities would be greatly appreciated.

      1 Reply Last reply Reply Quote 1
      • dgelessus
        dgelessus last edited by

        In general, it's not possible for one app to interact with another one directly - for example, there is no way to ask Safari what webpages are currently open in each tab. It's also not possible to read or write Safari's bookmarks (though Pythonista's version of the webbrowser module provides limited access to the reading list), and Apple's Notes app has no API at all (as far as I know).

        However, you can use Pythonista's share sheet extension to "share" a URL from Safari and run a Python script that does something with the URL. For example, your script could download the URL's contents and then process the HTML code to extract the text for example (the requests and bs4 modules are useful for that).

        To get the processed text back out of Pythonista, you can use the share sheet again (which you can invoke from your script with the dialogs module). Some apps also have custom URL schemes to perform various actions, which you can invoke using the webbrowser module. Apple's apps usually don't have custom URL schemes (or at least not very useful ones). Pythonista also includes an evernote module, though I have never used it myself, so I don't know how exactly it works.

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

          I found on the Apple Developer website as follows, I am not an iOS developer, but look at the introduction that can be achieved.

          Accessing a Webpage

          In Share extensions (on both platforms) and Action extensions (iOS only), you can give users access to web content by asking Safari to run a JavaScript file and return the results to the extension. You can also use the JavaScript file to access a webpage before your extension runs (on both platforms), or to access or modify the webpage after your extension completes its task (iOS only). For example, a Share extension can help users share content from a webpage, or an Action extension in iOS might display a translation of the user’s current webpage.

          To add webpage access and manipulation to your app extension, perform the following steps:

          Create a JavaScript file that includes a global object named ExtensionPreprocessingJS. Assign a new instance of your custom JavaScript class to this object.
          In the NSExtensionActivationRule dictionary in your app extension’s Info.plist file, give the NSExtensionActivationSupportsWebPageWithMaxCount key a nonzero value. (To learn more about the activation rule dictionary, see Declaring Supported Data Types for a Share or Action Extension.)
          When your app extension starts, use the NSItemProvider class to get the results returned by the execution of the JavaScript file.
          In an iOS app extension, pass values to the JavaScript file if you want Safari to modify the webpage when your extension completes its task. (You use the NSItemProvider class in this step, too.)
          To tell Safari that your app extension includes a JavaScript file, add the NSExtensionJavaScriptPreprocessingFile key to the NSExtensionAttributes dictionary. The value of the key should be the file that you want Safari to load before your extension starts. For example:

          <key>NSExtensionAttributes</key>
          <dict>
          <key>NSExtensionJavaScriptPreprocessingFile</key>
          <string>MyJavaScriptFile</string> <!-- Do not include the ".js" filename extension -->
          </dict>

          1 Reply Last reply Reply Quote 0
          • JonB
            JonB last edited by ccc

            The latest beta has an appex.get_webpage_info and appex.finish functions which did not exist previously. the first gets the refereer, title, html source, cookies, selected text and selected html. the latter allows you to execute a js snippet in safari.

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

              Thank you. I have installed the latest beta version today. But i always get an empty dictionary every time when i call appex.get_webpage_info(). Do you know why?

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

                Through the further testing, I found that some of web sites (such as stackoverflow.com) can return the expected results for the first run, but for the second run it always return the empty. Refresh the page will be able to return to the expected results. But other sites(such as stackoverflow.com/users/login ) can not return any results anyway.

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

                  @jumpbeen So far, I can't reproduce the behavior you're describing. Which iOS version are you using? What kind of device?

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

                    MNFW2CH/A(iPhone 7 Plus), iOS 10.3.1.
                    And i found out when the appex.get_web_page_info() return {}, the _appex.get_input() will return:

                    [{'attachments': [{'com.apple.property-list': 'Error: Unknown data type: (null)'}]}]

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