Applescript comparison stopped working - if-statement

I have a script that does a cURL request and returns a string if no database rows are found. At one point this was working and then all the sudden it stopped working.
set testURL to the quoted form of ("http:/
set theURL to (do shell script "curl " & testURL & "")
if theURL = "no rows" then
display dialog "error"
error number -128
else
display dialog("success")
end if
when i display the veriable setURL it always comes back "no rows" and the comparison never works. It skips over the error message and skips right to success no matter what. What is this and why did my comparison stop working all of the sudden? I have tried everything from trying to set it as a string to different ways of saying the same thing and it never works.

Maybe the result contains some whitespace.
Try
theURL contains "no rows"
theURL starts with "no rows"
theURL ends with "no rows"
By the way: Why do you append an empty string to your do shell script line?

Related

Assigning values to a list of variables without using an if statement in Applescript

This question seems to have been covered before in other languages, but I can't seem to locate one in applescript. I'm making a script that allows me to bring up a menu and select a website to go to. I currently have it working, but it uses an if statement to go through every possible option to see if that's the one I chose -- you can see below:
set theDestinations to {"Agorapulse", "Applescript Error Guide", "Budget Template", "Chinese Dictionary", "FNBG Email", "Key Codes", "Slacker", "Verbling - Chinese", "Verbling - Russian"} as list
set theChosenDestination to choose from list theDestinations with prompt "Where do you want to go?" default items {"Agorapulse"}
set theChosenDestination to (get item 1 of theChosenDestination)
tell application "System Events"
if (theChosenDestination = "Agorapulse") then
set theURL to "https://app.agorapulse.com/"
else if (theChosenDestination = "Applescript Error Guide") then
set theURL to "http://krypted.com/lists/comprehensive-list-of-mac-os-x-error-codes/"
else if (theChosenDestination = "Budget Template") then
set theURL to "https://docs.google.com/spreadsheets/d/1X4Yso2DgTbJq_c807_gQhuZBNa5sKtYc1VYEzHfZVnU/edit#gid=1442879437"
else if (theChosenDestination = "Chinese Dictionary") then
set theURL to "https://www.mdbg.net/chinese/dictionary?page=worddict&wdrst=0&wdqb=hedgehog"
else if (theChosenDestination = "FNBG Email") then
set theURL to "https://mail.google.com/mail/u/0/#label/FNBGames"
else if (theChosenDestination = "Key Codes") then
set theURL to "https://eastmanreference.com/complete-list-of-applescript-key-codes"
else if (theChosenDestination = "Slacker") then
set theURL to "https://slacker.nathanhoad.net/fnbgames"
else if (theChosenDestination = "Verbling - Chinese") then
set theURL to "https://www.verbling.com/lesson/5c9007a743a3330007554ad8"
else if (theChosenDestination = "Verbling - Russian") then
set theURL to "https://www.verbling.com/lesson/5c9b354fd50da2000708901a"
else if (theChosenDestination = "Other") then
set theURL to text returned of (display dialog "Enter the URL of the target website:" default answer "")
else
display dialog "no match"
end if
end tell
tell application "Google Chrome"
activate
open location theURL
end tell
Like I said, it works, and given the light work it has to do it operates plenty fast, but it is inelegant. I look at that long list.
What I'd really like to be able to do is something like I've seen in other languages, something like a list of {("Agorapulse", "https://app.agorapulse.com"),(...)}, but I've fiddled about and can't seem to make it work. Any thoughts or help would be greatly appreciated.
(oh, also, I had to beat the code about the head and shoulders a bit to get the list choices and the checks in the if statement to be of the same class so they'd talk to each other. I think I did an OK job of that, but feel free to critique :)
Use two lists, adding the index prefix is more efficient.
set theDestinations to {"1 Agorapulse", "2 Applescript Error Guide", "3 Budget Template", "4 Chinese Dictionary", "5 FNBG Email", "6 Key Codes", "7 Slacker", "8 Verbling - Chinese", "9 Verbling - Russian", "10 Other…"}
set theURLs to {"https://app.agorapulse.com/", "http://krypted.com/lists/comprehensive-list-of-mac-os-x-error-codes/", "https://docs.google.com/spreadsheets/d/1X4Yso2DgTbJq_c807_gQhuZBNa5sKtYc1VYEzHfZVnU/edit#gid=1442879437", "https://www.mdbg.net/chinese/dictionary?page=worddict&wdrst=0&wdqb=hedgehog", "https://mail.google.com/mail/u/0/#label/FNBGames", "https://eastmanreference.com/complete-list-of-applescript-key-codes", "https://slacker.nathanhoad.net/fnbgames", "https://www.verbling.com/lesson/5c9007a743a3330007554ad8", "https://www.verbling.com/lesson/5c9b354fd50da2000708901a"}
set theChosenDestination to choose from list theDestinations with prompt "Where do you want to go?" default items {"1 Agorapulse"}
if theChosenDestination is false then return
set chosenIndex to (word 1 of item 1 of theChosenDestination) as integer
if chosenIndex < 10 then
set theURL to item chosenIndex of theURLs
else
set theURL to text returned of (display dialog "Enter the URL of the target website:" default answer "")
end if

SQLite 3 Error when using parameter string, but not when implicitly typed

I have a snippet of code from my python 2.7 program:
cur.execute("UPDATE echo SET ? = ? WHERE ID = ?", (cur_class, fdate, ID,))
that when run, keeps throwing the following error:
sqlite3.OperationalError: near "?": syntax error
The program is supposed to insert today's date, into the class column that matches the student ID supplied. If I remove the first "?" like so and hard code the parameter:
cur.execute("UPDATE echo SET math = ? WHERE ID = ?", (fdate, ID,))
everything works just fine. I've googled all over the place and haven't found anything that works yet so I'm throwing out a lifeline.
I've tried single quotes, double quotes, with and without parenthesis and a few other things I can't remember now. So far nothing works other than hard coding that first parameter which is really inconvenient.
As a troubleshooting step I had my program print the type() of each of the variables and they're all strings. The data type of the the cur_class field is VARCHAR, fdate is DATE, and ID is VARCHAR.
Thanks to the tip from #Shawn earlier I solved the issue with the following code and it works great:
sqlcommand = "UPDATE echo SET " + cur_class + " = " + fdate + " WHERE ID = " + ID
cur.execute(sqlcommand)
This way python does the heavy lifting and constructs my string with all the variables expanded, then has the db execute the properly formatted SQL command.

If Statement for Function looping incorrectly

I am trying to recreate a simple program I made once in Python in VBScript to have it run in a dialog box.
This is the code:
Set objShell = CreateObject("Wscript.Shell")
Function Query()
query = InputBox("Please input the Subreddit you with to navigate to e.g (globaloffensive)", "iixCarbonxZz's Subreddit Finder")
End Function
Do
If query = "" Then
err = MsgBox("Please enter a Valid Subreddit", vbOKOnly + vbExclamation, "Invalid")
Else
objShell.Run("http://www.reddit.com/r/" & query)
WScript.Quit()
End If
Query()
Loop
My problem is this:
The code opens the input text box and if there is nothing in the text box when the OK button is pressed it should bring up the 'err' error message box and then loop. If it gets something in the input box it should use it then close the script.
In practice if I leave the box blank it displays the message, loops back to the input box but then if left blank again skips the if statement and just reloads the input box. If something is input that it can use it skips the if statement and reloads the box the first time and then runs and closes the second time. If prior to this a blank was submitted it will skip the if statement one further time before using the input properly.
Use a variable to store the function's result
Don't use the same name for the variable and the function
Demo:
Option Explicit
Function getQuery()
getQuery = InputBox("Please input the Subreddit you with to navigate to e.g (globaloffensive)", "iixCarbonxZz's Subreddit Finder")
End Function
Dim query
Do
query = getQuery()
If query = "" Then
err = MsgBox("Please enter a Valid Subreddit", vbOKOnly + vbExclamation,"Invalid")
Else
WScript.Echo "http://www.reddit.com/r/" & query
WScript.Quit 0
End If
Loop

How to manage looping on this list on Applscript?

The list is in the form of:-
0:
https://url
1:
https://url
..... And so on.
How could I loop on this list. So I could fetch the number first without ":" and type it somewhere then fetch the url that comes after that number and type it elsewhere. Then end repeat if the list is over.
Or should I use records instead?
I am still a beginner using AppleScript. I tried many commands I mixed up but the computer keeps running the script nonestop and the activity monitor shows the applescript using 100% of the processor and huge amount of ram.
Appreciate any help.
Thank you
You didn't define what your list really looks like very well so I made an assumption on my answer below. If I was wrong, hopefully my answer will at least point you in the right direction. (or if I've gotten it wrong, but you can choose to reformat it to the way I suggested, that could still help)
on run
set theList to {"0:http://apple.com", "1:http://google.com"} -- my guess at what your list looks like.
repeat with anItem in theList
set anItem to anItem as string
set itemParts to myParseItem(anItem)
set tID to the_integer of itemParts as integer
set tURL to the_url of itemParts as string
end repeat
end run
on myParseItem(theItem)
set AppleScript's text item delimiters to ":"
set delimitedList to every text item of theItem
set newString to (items 2 thru -1 of delimitedList as string)
set AppleScript's text item delimiters to {""}
set theInt to item 1 of delimitedList
set theURL to newString as string
return {the_integer:theInt, the_url:theURL}
end myParseItem

Nested IF, INDEX and MATCH in Google Sheets

I'm trying to return a value in Google sheets.
This is done using an Index Match as follows, which does work:
=iferror(index(Data!B:B, match(B5339,Data!G:G,0)),"Not Found")
I'd now like to expand this, so that if this first test fails, try looking up that same data in another sheet....
=iferror(if(index(Data!B:B, match(B5340,Data!G:G,0),if(index(HeadOfficeLeads!B:B, match(B5340,HeadOfficeLeads!A:A,0))))),"Not found")
This outputs the fail msg of "Not Found".
However, although the first test is indeed false, the second test is true (this second data set does in fact hold a match).
NB - the data containing this correct match on the 2nd sheet is created by a UNIQUE ( FILTER, FWIW....
For some reason, it doesnt look like the second IF statement is being run - and the whole thing doesnt work, giving the error "Wrong number of arguments".
I have a feeling the argument issue is that the first test doesnt have an "if false" clause - but believe the "IFERROR" parent should handle this?
If not, where would I put the "if false clause" for the IF's?
You don't need any if, because iferror already contains an if statement in its logic (as its name suggests). Here is an example of nested iferror statements, simplified for clarity:
=iferror(match("a", A1:A5, 0), iferror(match("a", B1:B5", 0), "not found"))
This will return the position of "a" in column A, if it's there; otherwise, it will return its position in column B if it's there, otherwise it returns "not found".
Works the same with index or anything else around match function.