Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use AppleScript version "2.5"
- use framework "Foundation"
- use scripting additions
- property |⌘| : a reference to current application
- 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"}, ¬
- {"imdb", "https://www.imdb.com/find?q={search}&s=all"}, ¬
- {"maps", "https://maps.google.com/maps?hl=en&authuser=0&q={search}&ie=UTF-8"}, ¬
- {"we", "https://en.wikipedia.org/w/index.php?title=Special:Search&search={search}"}, ¬
- {"yt", "https://www.youtube.com/results?search_query={search}"}}
- -- Get the existing clipboard contents.
- set oldClipboard to (the clipboard)
- -- Ensure Safari's the frontmost process.
- activate application "Safari"
- tell application "System Events"
- set frontmost of application process "Safari" to true
- -- Select and copy the address field contents.
- keystroke "lc" using {command down}
- end tell
- -- Keep getting the clipboard contents until they change or haven't changed after a certain time. (Adjust as required.)
- repeat 2 times
- set textValue to (the clipboard)
- if (textValue is not oldClipboard) then exit repeat
- delay 0.5
- end repeat
- -- Restore the old contents. (Optional.)
- set the clipboard to oldClipboard
- set spaceOffset to offset of space in textValue
- if spaceOffset = 0 then return
- set token to text 1 thru (spaceOffset - 1) of textValue
- set query to text (spaceOffset + 1) thru -1 of textValue
- set nsQuery to |⌘|'s NSString's stringWithString:query
- set allowedPathCharacterSet to |⌘|'s NSCharacterSet's URLPathAllowedCharacterSet()
- set encodedQuery to nsQuery's stringByAddingPercentEncodingWithAllowedCharacters:allowedPathCharacterSet
- repeat with aShortcut in shortcuts
- set {_token, _url} to contents of aShortcut
- if _token is token then
- set queryURL to (|⌘|'s NSString's stringWithString:_url)
- set searchURL to (queryURL's stringByReplacingOccurrencesOfString:"{search}" withString:encodedQuery)
- tell application "Safari" to set URL of current tab of window 1 to (searchURL as text)
- exit repeat
- end if
- end repeat
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement