Guest User

Safari 13 auf dem Mac per Automator wieder eigene Suchmaschi

a guest
Mar 22nd, 2020
114
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use AppleScript version "2.5"
  2. use framework "Foundation"
  3. use scripting additions
  4.  
  5. property |⌘| : a reference to current application
  6. property shortcuts : {{"ama", "https://www.amazon.com/s/?link_code=wsw&_encoding=UTF-8&search-alias=aps&field-keywords={search}&Submit.x=0&Submit.y=0&Submit=Go"}, ¬
  7. {"imdb", "https://www.imdb.com/find?q={search}&s=all"}, ¬
  8. {"maps", "https://maps.google.com/maps?hl=en&authuser=0&q={search}&ie=UTF-8"}, ¬
  9. {"we", "https://en.wikipedia.org/w/index.php?title=Special:Search&search={search}"}, ¬
  10. {"yt", "https://www.youtube.com/results?search_query={search}"}}
  11.  
  12. -- Get the existing clipboard contents.
  13. set oldClipboard to (the clipboard)
  14. -- Ensure Safari's the frontmost process.
  15. activate application "Safari"
  16. tell application "System Events"
  17. set frontmost of application process "Safari" to true
  18. -- Select and copy the address field contents.
  19. keystroke "lc" using {command down}
  20. end tell
  21. -- Keep getting the clipboard contents until they change or haven't changed after a certain time. (Adjust as required.)
  22. repeat 2 times
  23. set textValue to (the clipboard)
  24. if (textValue is not oldClipboard) then exit repeat
  25. delay 0.5
  26. end repeat
  27. -- Restore the old contents. (Optional.)
  28. set the clipboard to oldClipboard
  29.  
  30. set spaceOffset to offset of space in textValue
  31. if spaceOffset = 0 then return
  32. set token to text 1 thru (spaceOffset - 1) of textValue
  33. set query to text (spaceOffset + 1) thru -1 of textValue
  34.  
  35. set nsQuery to |⌘|'s NSString's stringWithString:query
  36. set allowedPathCharacterSet to |⌘|'s NSCharacterSet's URLPathAllowedCharacterSet()
  37. set encodedQuery to nsQuery's stringByAddingPercentEncodingWithAllowedCharacters:allowedPathCharacterSet
  38. repeat with aShortcut in shortcuts
  39. set {_token, _url} to contents of aShortcut
  40. if _token is token then
  41. set queryURL to (|⌘|'s NSString's stringWithString:_url)
  42. set searchURL to (queryURL's stringByReplacingOccurrencesOfString:"{search}" withString:encodedQuery)
  43. tell application "Safari" to set URL of current tab of window 1 to (searchURL as text)
  44. exit repeat
  45. end if
  46. end repeat
RAW Paste Data