EDIT: I’ve just thought of another way to tackle this problem that avoids putting the Login Request into the handle_string handler, so no need to solve this one, I think I’ll be OK.
Hello,
I’m getting some help with another part of my Twitter Updating script on another thread, but while I’m waiting for an answer to my last question, I decided to work on another part of my script and ran into a different problem. (I hope it’s not bad etiquette to open multiple threads on the same project… If so, please let me know and I won’t do it again. )
Anyway, I thought it would be nice for the user experience if my script asked for their Twitter username and password and then stored it in some properties so they wouldn’t have to do it again. I wrote this and it worked fine on its own:
property TwitterUser : ""
property TwitterPassword : ""
if TwitterUser is "" or TwitterPassword is "" then
repeat
display dialog "Please enter your Twitter username. (You should only have to do this once.)" default answer ""
if (text returned of result) is not "" then
set TwitterUser to text returned of result
display dialog TwitterUser
exit repeat
else
display dialog "You didn't enter a username."
end if
end repeat
repeat
display dialog "Please enter your Twitter password" default answer ""
if (text returned of result) is not "" then
set TwitterPassword to text returned of result
display dialog TwitterPassword
exit repeat
else
display dialog "You didn't enter a password."
end if
end repeat
else
display dialog TwitterUser & return & TwitterPassword
end if
But the ultimate goal was to include it in a script that would be launched using LaunchBar, whose behavior is to prompt you for text after you call up the script and hit the space bar and then (I think I’m understanding this correctly) uses the handler handle_string() to send your text along to an AppleScript to use. My problem is that when I placed the above scriptlet into the script that responds to that handler, the properties seem to no longer persist. They work fine within that script – I can use them to send out a Tweet after they are entered – but the next time the script is run you have to enter the same information all over again.
My question is – is there something I’m missing that would get the property values to be persistent? Or is my only solution to save the username and password in a text file to be read again in the future?
For reference, here is the relevant section of the script in question:
property TwitterUser : ""
property TwitterPassword : ""
on handle_string(s)
display dialog TwitterUser & return & TwitterPassword
-- First, check the character limit. If the tweet is over the limit, give an error with number of characters in the attempted Tweet.
set theCharacterCount to count (s)
if theCharacterCount is greater than 140 then
display dialog "Error: more than 140 characters (" & theCharacterCount & " characters in total)"
--If the Tweet is within the character limit, get the userinfo and turn the Tweet into an instruction to update Twitter using cURL!
else
GetUserInfo()
set TwitterUpdate to "curl -u " & TwitterUser & ":" & TwitterPassword & " -d status=\"" & s & "\" [url=http://twitter.com/statuses/update.xml]http://twitter.com/statuses/update.xml"[/url]
UpdateTwitter(TwitterUpdate, s)
end if
end handle_string
on GetUserInfo()
-- check to see if the username and password have already been entered
if TwitterUser is "" or TwitterPassword is "" then
-- if not, then request them
repeat
display dialog "Please enter your Twitter username. (You should only have to do this once.)" default answer ""
if (text returned of result) is not "" then
set TwitterUser to text returned of result
display dialog TwitterUser
exit repeat
else
display dialog "You didn't enter a username."
end if
end repeat
repeat
display dialog "Please enter your Twitter password" default answer ""
if (text returned of result) is not "" then
set TwitterPassword to text returned of result
display dialog TwitterPassword
exit repeat
else
display dialog "You didn't enter a password."
end if
end repeat
else
-- if so, display them
display dialog TwitterUser & return & TwitterPassword
end if
end GetUserInfo
Is there something this beginning AppleScripter is missing? Thanks in advance for your help!
Alex