0

I have no experience with Swift , but my organisation don't want anyone to run AppleScript anymore. I'll be alright using swift though

I was hopping to be able to get text from safari on a desktop swift app

for e.g

property leftEdge5 : "class=\"value\">"
property rightEdge5 : "</span>"


tell application "Safari"

    set TheTransactionIDGraber to do JavaScript "document.getElementsByClassName('field emphasizedid')[0].innerHTML;" in current tab of window 1

end tell

set transactionID to ""
set theText to Unicode text
set theSource to TheTransactionIDGraber
try
    set saveTID to text item delimiters
    set text item delimiters to leftEdge5
    set classValue to text item 2 of theSource
    set text item delimiters to rightEdge5
    set transactionID to text item 1 of classValue
    set text item delimiters to saveTID
    transactionID
end try
set the clipboard to "PID: " & transactionID

is they an easy way to do the same with Swift ? or get swift to run a basic applescript and store the value ? perhaps in a plist file

Kevin
  • 1,076
  • 4
  • 22
  • 49
  • Possible duplicate of [Can you execute an Applescript script from a Swift Application](https://stackoverflow.com/questions/25163433/can-you-execute-an-applescript-script-from-a-swift-application) – Joakim Danielson Nov 25 '19 at 17:21
  • @Joakim Danielson, I wouldn't say this is a duplicate question. The idea about running an AppleScript from within Swift was presented by the OP as an after thought and, actually, is not the way I would consider for this task (especially as he also stated his company doesn't want him to use AppleScript, so using AppleScript would seem like a bad idea). – CJK Nov 26 '19 at 01:31
  • @Kevin, If you company said not to use AppleScript, I wouldn't suggest using AppleScript, even inside a Swift script. You can actually bypass _Safari_ completely and use Apple/s `WebKit` API. The `WKWebView` class methods allow you to load web pages either in a viewport (a window or dialog you create), or without one so it loads in the background invisibly but retains full functionality to manipulate the DOM, eg. the [**`evaluateJavaScript()`**](https://developer.apple.com/documentation/webkit/wkwebview/1415017-evaluatejavascript) method that belongs to a `WKWebView` instance. – CJK Nov 26 '19 at 01:39
  • It looks like your AppleScript is scraping some information from a webpage. Have you checked if the site provides a web service for accessing that information? That would be much more reliable. If not, have you tried searching the web for the obvious keywords? e.g. `Swift web page scraping` brings up a bunch of promising links. – foo Nov 26 '19 at 20:23

0 Answers0