Can't use "open location" when it's part of a longer script

The following script works fine to open a URL in my default browser:

open location "https://google.com"

However, I've got an existing script to grab a URL from my Terminal window to open it in my browser. When I use this script, I get the error "Expected end of line but found identifier" and the variable for my URL was highlighted:

use framework "Foundation"

tell application "Terminal"
    set windowContents to the contents of the front window
end tell
set windowContents to current application's class "NSString"'s stringWithString:windowContents
set fidoRegexPattern to "<PATTERN>"
set fidoRegex to current application's class "NSRegularExpression"'s regularExpressionWithPattern:(fidoRegexPattern) options:(0) |error|:(missing value)
set matchRanges to (fidoRegex's matchesInString:(windowContents) options:(0) range:({0, windowContents's |length|()}))'s valueForKey:("range")

set urlStrings to current application's class "NSMutableArray"'s new()
repeat with thisRange in matchRanges
    tell urlStrings to addObject:(windowContents's substringWithRange:(thisRange))
end repeat
-- return urlStrings as list
set urlList to urlStrings as list
set fidoUrl to the last item of urlList
open location fidoUrl
return fidoUrl

Even if I try swapping out fidoUrl for a URL string literal, I'll get the same error, except it'll say that it found " instead of end of line.

Everything else about the script looks fine. If I take out the open location line and look at the return line's results, it gives me the URL I'm expecting.

Googling around seems to indicate that I might get this error if there's an issue with the script itself, but I'm not seeing anything.


Solution 1:

Anytime you add a use statement to a script,scripting additions is no longer added by default. It needs to be re-added.

Add the following after use framework "Foundation"

 use scripting additions

If for some reason you do not want scripting additions commands available throughout your entire script, this following option will work as well.

using terms from scripting additions
    open location "https://google.com"
end using terms from