First of all, your first line of code tell application "Safari" to set the content of var1 to URL of active tab of front window
doesn't even compile! It errors out with "Syntax Error A class name can’t go after this identifier." pointing to active tab
. So, do shell script "grep " & var1 & "path-to-my-file/file"
could't possibly work as written anyway!
A proper way to get the URL
would be:
tell application "Safari" to get the URL of current tab of window 1 of application "Safari"
Or to set it to a variable, use:
tell application "Safari" to set theURL to (get the URL of current tab of window 1 of application "Safari")
So, the first two lines of code in your question would work if used like:
try
tell application "Safari" to set theURL to (get the URL of current tab of window 1 of application "Safari")
do shell script "grep " & quoted form of theURL & " /path/to/target/file"
end try
Note there is a space after the double-quote before /path/to/target/file
and this space is necessary.
Also the code is wrapped in a try
statement to trap any errors. Although you'd need to add additional code to process the error(s) accordingly using an on error
handler. While the try
statement will hide an error, using it in conjunction with an on error
handler will be necessary if grep
doesn't find a match and you want to do additional processing based on the results from the grep
command not finding a match.
If you really want to do it without setting the URL
to a variable, then use this method:
try
do shell script "grep $(osascript -e 'tell application \"Safari\" to get the URL of current tab of window 1 of application \"Safari\"')" & " /path/to/target/file"
end try
Note that in this case the necessary space can be as shown, after the double-quote before the pathname, or it can be placed after the closing parentheses of $(...)
and before the closing double-quote prior to the ampersand, e.g: ...\"Safari\"') " &