Could find anything remotely close via search so I apologize if post exists somewhere.
Here is what I would like to accomplish have a dialog prompt for a date (ie. What date would you like look up?). Format would be standard US m/d/yy, ( 8/9/05 ). Then take that date and convert it to a valid URL format ( 8%2F9%2F05 ) and insert it into a URL …/historical/default.asp?detect=1&date=8%2F9%2F05&x=0&y=0…
Once the URL is constructed open that puppy in Safari (that’s the easy part I think)
Bonus if you can throw in 2 inputs on the dialog 1 for the date and the other for another variable that is only letters (no spaces or #'s or special characters)
Presto changeo Save about 5 clicks and some typing since I do this just about 20 times a day
Thx,
Chip
AppleScript: 2.1
Browser: Safari 412.2
Operating System: Mac OS X (10.4)
try
set the_date to short date string of date (text returned of (display dialog "Enter a date:" default answer ""))
on error error_message number error_number
display dialog "Error: " & error_number & return & error_message
end try
set text item delimiters to "/"
set the_date to text items of the_date
set text item delimiters to "%2F"
set the_date to the_date as string
set the_url to "./historical/default.asp?detect=1&date=" & the_date & "&x=0&y=0.."
When the user hits cancel, how can I have it bypass the error dialog? I only want it to show the error if it a true error. Maybe just a dialog that says “You canceled data retrieval. Have a nice day.” then end the script.
If input is blank (“”) then I would like a dialog that says “You did not enter a date. Please try again” when OK clicked then it goes back to the Date dialog
If the date is not valid then a dialog that says “That is not a valid date. Please enter a valid date.” when OK clicked goes back to Date dialog.
As you can tell I know nothing about error handling.
This addresses point 1 and, on the basis that a blank entry can also be described as invalid, rolls points 2 & 3 together:
to getDate(m, d)
set d to text returned of (display dialog m default answer d)
try
tell (short date string of date d)'s words to ¬
beginning & "%2" & middle item & "%2" & end
on error
getDate("Invalid entry. Please enter a valid date:", d)
end try
end getDate
open location ".../historical/default.asp?detect=1&date=" & getDate("Enter a date:", "") & "&x=0&y=0..."
Oh yeah. The old eyesight’s not what it used to be…
Perhaps I can break it down for you, once we’ve got a version that suits. I’m not entirely sure what you mean about adding previous days date. Can you enlarge a little, maybe?
To illustrate let’s use today as a reference (8/13/05)
Instead of the defaul date being blank (“”) I would like the date to fill with 8/12/05 (yesterdays date) since usually it is the previous day I am looking for, not always but usually and since the whole date is highlighted when the dialog comes up if the date I want isn’t yesterday I can just type the date I want.
Addition to the above would have another prompt once the first date is processed saying “would you like to retrieve another date?” No(default) Yes
If answer is No then quits if Yes then another date dialog comes up (just like before, which I think you can do with the subroutine that is already there (???)) and open that new date in a new tab.
This is what I have so far but I can’t get the date dialog to come up and I am not sure I am passing the “Yes” correctly.
to getDate(m, d)
set d to text returned of (display dialog m default answer d buttons {"Cancel", "Get Quote"} default button 2 with icon2)
try
tell (short date string of date d)'s words to ¬
beginning & "%2F" & middle item & "%2F" & end
on error
getDate("Invalid entry. Please enter a valid date:", d)
end try
end getDate
tell application "System Events" to keystroke "t" using command down
open location "...date=" & getDate("Enter a date:", "") & "&x=..."
tell application "Safari"
if button returned of (display dialog "Would you like to retrieve data for another date?" buttons {"No", "Yes"} default button 1 with icon 1) is "Yes" then getDate
end tell
Event Log:
{text returned:"8/2/05", button returned:"Get Quote"}
open location "...date=8%2F2%2F05&x=..."
end tell
tell application “Safari”
display dialog “Would you like to retrieve data for another date?” buttons {“No”, “Yes”} default button 1 with icon 1
{button returned:“Yes”}
end tell
set dialog_text to "Please enter a date:"
repeat
set the_date to text returned of (display dialog dialog_text default answer (short date string of ((current date) - days)))
try
tell short date string of date the_date to set formatted_date to word 1 & "%2F" & word 2 & "%2F" & word 3
open location "./historical/default.asp?detect=1&date=" & formatted_date & "&x=0&y=0.."
if button returned of (display dialog "Would you like to retrieve data for another date?" buttons {"No", "Yes"} default button "No") is "No" then exit repeat
on error error_message
set dialog_text to error_message & return & "Please enter a valid date:"
end try
end repeat
Ah - got it now, Chip. Sorry for being so obtuse - but it was way past my bedtime.
One word of explanation might help to clarify structure here: You’ll see that the essential difference in approaches is that Qwerty’s version uses a repeat loop, while mine opts for a recursive handler (in other words, the ‘getDate’ handler actually calls itself). The result is pretty much the same, and choice depends largely on circumstances and preference.
I liked kel’s suggestion to directly convert the text as entered. However, coercing to a date should ensure that all entry errors are trapped. In addition (as you may already know), including a date coercion means your entries could be even shorter than the standard m/d/y format. In cases like this, AppleScript will try to guess the most appropriate date - even if the entry is only one integer.
I think the following version should do pretty much what you want. It activates the script on each recursion to keep it frontmost - allowing you to make multiple entries. Once you hit the Cancel button, it should finally switch to activate Safari. (I’ve also tried to minimise the number of dialogs involved.)
to getDate(m, d)
activate
set d to text returned of (display dialog m default answer d)
try
tell short date string of date d to set l to ".../historical/default.asp?detect=1&date=" & ¬
word 1 & "%2F" & word 2 & "%2F" & word 3 & "&x=0&y=0..."
open location l
getDate("Enter another date:", d)
on error number n
if n is -128 then
tell application "Safari" to activate
else
getDate("Please enter a valid date:", d)
end if
end try
end getDate
getDate("Please enter a date:", short date string of ((current date) - days))
Chip - I didn’t notice that you apparently require an explicit ‘new tab’ command. The following version also keeps Safari frontmost, which is probably preferable.
(Incidentally, testing would be a darned sight easier if you could give us the actual URL in question - or is it a private site?)
to getDate(m, d)
tell application "Safari" to set d to text returned of (display dialog m default answer d)
try
tell short date string of date d to set l to ".../historical/default.asp?detect=1&date=" & ¬
word 1 & "%2F" & word 2 & "%2F" & word 3 & "&x=0&y=0..."
tell application "System Events" to keystroke "t" using command down
open location l
getDate("Enter another date:", d)
on error number n
if n is not -128 then getDate("Please enter a valid date:", d)
end try
end getDate
tell application "Safari" to activate
getDate("Please enter a date:", short date string of ((current date) - days))
True - but since we were in on a recent discussion about this very subject (which NG first pointed out), I’m assuming that Chip has his own reasons for doing it this way…