PDA

View Full Version : Applescript - newbie help




CorbuCorbu
Feb 26, 2007, 05:20 AM
Hey,

I'm new to Applescript and programming in general, and I'm trying to write my first script.

The idea is to write something that will sign into my bank's website and download the last week's worth of transactions - Quicken is surprisingly unhelpful in doing this neatly.


I got stuck when I tried to have the script enter last week's date in a text field. After searching around a little, I found a shell script which I modified to suit my format:


do shell script "date '+%m/%d/%Y'"


and used "keystroke result" to get Firefox to type out the date.


SO,

How do I convert the date to last week's date? I would just subtract 7 from the "days" section, but I have no idea how the shell script operates. I realize this is probably very basic stuff, but I'd appreciate any help or examples. Thanks.



lancestraz
Feb 26, 2007, 01:13 PM
I'm probable going at this entirely the wrong way.... But try this code:
set the_date to (one_week_ago())
tell application "System Events"
tell process "Fire Fox"
keystroke the_date
end tell
end tell




-----SUBROUTINE-----
on one_week_ago()
set month_list to {"31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"}
set the_date to (do shell script "date '+%m/%d/%Y'")
set the_year to items 7 thru 10 of the_date
set the_day to items 4 thru 5 of the_date as string
set the_month to items 1 thru 2 of the_date as string
set the_month_days to (item the_month of month_list)
if the_day - 7 is less than 1 then
set old_month to (the_month - 1)
if old_month is 0 then
set old_month to "12"
set the_year to (the_year - 1)
end if
set old_month_days to (item old_month of month_list)
set old_month to (format old_month into "0#")
set old_day to ((the_day - 7) * -1)
set old_day to (old_month_days - old_day)
set the_date to (old_month & "/" & old_day & "/" & the_year)
else
set the_day to the_day - 7
set the_day to (format the_day into "#00")
set the_past_date to (the_month & "/" & the_day & "/" & the_year)
end if
end one_week_ago

CorbuCorbu
Feb 26, 2007, 01:32 PM
Hmmm... that returned a "Expected "," but returned identifier" message, referring to the line:

set old_month to (format old_month into "0#")


ideas?

lancestraz
Feb 26, 2007, 03:33 PM
Try this:

set the_date to (one_week_ago())
display dialog the_date






-----SUBROUTINE-----
on one_week_ago()
set month_list to {"31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"}
set the_date to (do shell script "date '+%m/%d/%Y'")
set the_year to items 7 thru 10 of the_date as string
set the_day to items 4 thru 5 of the_date as string
set the_month to items 1 thru 2 of the_date as string
set the_month_days to (item the_month of month_list)
if the_day - 7 is less than 1 then
set old_month to (the_month - 1)
if old_month is 0 then
set old_month to "12"
set the_year to (the_year - 1)
end if
set old_month_days to (item old_month of month_list)
set old_month_string to old_month as string
set the_number to count (old_month_string)
if the_number is 1 then
set old_month to ("0" & old_month)
end if
set old_day to ((the_day - 7) * -1)
set old_day to (old_month_days - old_day)
set the_date to (old_month & "/" & old_day & "/" & the_year)
set the_date to the_date as string
else
set the_day to the_day - 7
set the_number to (count the_day)
if the_number is 1 then
set the_day to ("0" & the_day)
end if
set the_date to (the_month & "/" & the_day & "/" & the_year)
set the_date to the_date as string
end if
end one_week_ago

Does it display a dialog box?

I think the I installed an AppleScript plugin that included the "format" command.
If this is the case it wont work for you.
Lets see if there's a workaround...

CorbuCorbu
Feb 26, 2007, 04:43 PM
it worked!

thanks so much... i have a lot in mind for this script, but strangely enough this seemed to be the hardest part. i'll come back here if i get stuck again.

j

ChrisA
Feb 26, 2007, 06:05 PM
Hey,

How do I convert the date to last week's date? I would just subtract 7 from the "days" section, but I have no idea how the shell script operates. I realize this is probably very basic stuff, but I'd appreciate any help or examples. Thanks.

You can't simply subtract 7 from days. What if it's the 5th of May? Then you'd have "the negative 2nd or May" Technically correct in a way not not conventional. In UNIX systems the time is kept as the number of second past 1970. Get that number and subtract 7 days worth of seconds from it and then print that time. This will handle al the odd cases like leap years and change overs between daylight savings.

[edit] see next post for examle code.

ChrisA
Feb 26, 2007, 06:50 PM
I'm probable going at this entirely the wrong way.... But try this code:

Talk about re-inventing the wheel...

The system provide functions to compute time. The problem is accessing them. The bash shell is limited. Perl however makes it easy. (In my last post I said to use date twice. That only works it seems for an odd version of date.)

To print the time in the current time zone 10,000 seconds in the past try this.


perl -e 'use POSIX;$t = strftime "%a %b %e %H:%M:%S %Y", localtime(time-10000); print $t;'

This will handle all the odd ball cases concerning leap years and daylight time changes and will track future policy changes because it reads them out of a system file that gets patched periodically.

Put the above perl in the place were you used date. Note that you can change the formatting string to suit. See man page "perlfunc" for details

CorbuCorbu
Feb 28, 2007, 12:39 AM
i'll try that. simple is good... thanks chris.

bullittube
Jun 9, 2008, 08:30 AM
hi i am a applescript noob and trying to learn it a bit better but i have got stuck i am trying to make a little script so when i run it it comes up with a text input box and i type a password and if it is right it says right and if i was wrong it beeps i know this is pointless but i would love to be able to do it so please help :)

lancestraz
Jun 9, 2008, 12:45 PM
hi i am a applescript noob and trying to learn it a bit better but i have got stuck i am trying to make a little script so when i run it it comes up with a text input box and i type a password and if it is right it says right and if i was wrong it beeps i know this is pointless but i would love to be able to do it so please help :)
The code is pretty self explanatory.
repeat
display dialog "Password:" default answer "" buttons {"Cancel", "OK"} default button 2 with icon 0 with hidden answer
if the text returned of the result is "pass" then
say "right"
exit repeat
else
beep
end if
end repeat