script vs. applescript

educated_foo on 2004-10-01T04:27:43

So I need something to kill time during the debates and further increase my level of frustration. Safari doesn't have a saved sessions. Let's try a bit of applescript... tell application "Safari" set urls to URL of every document -- tbd: how do I write a file in AS? end tell

No love -- this only saves the first tab of each window. Curse. Google around a bit. Curse a bit more when I find out what's involved, and when I can't make the cut-and-paste code to write a file work... #!/usr/bin/env perl

sub osascript($) { my $cmd = shift; local *O; open O, "|osascript > /tmp/osa.$$"; $cmd =~ s/\n/\r/g; print O $cmd; close O; open IN, "/tmp/osa.$$" or die $!; unlink "/tmp/osa.$$"; return split /\r/, join '', <IN>; }

sub get_docs { osascript <<'END'; tell application "System Events" set urls to {} tell process "Safari" repeat with w in every window tell w repeat with b in every radio button click b tell application "Safari" set urls to urls & (URL of first document) end tell end repeat end tell end repeat end tell set old_delim to AppleScript's text item delimiters set AppleScript's text item delimiters to return set urls to urls as text set AppleScript's text item delimiters to old_delim urls end tell END }

sub restore_docs { my $docthing = '{'.(join ',', map { qq{"$_"} } grep $_, @_).'}'; osascript <<END; tell application "System Events" set urls to $docthing tell application "Safari" repeat with u in urls open location u end repeat end tell end tell END }

@ARGV = grep $_,@ARGV; # wtf: quicksilver passes us an empty arg. if (@ARGV) { $op = shift; } else { $pf = "$ENV{HOME}/.safari-state"; if ($0 =~ /safari-save(?:\.pl)?$/) { $op = 'save'; open STDOUT, ">$pf" or die "$0: $pf: $!"; } elsif ($0 =~ /safari-load(?:\.pl)?$/) { $op = 'load'; open STDIN, $pf or die $!; } else { die; } }

if ($op =~ /^s/) { print STDOUT "$_\n" for get_docs; } elsif ($op =~ /^l/) { my @docs = map {chomp;$_} <STDIN>; restore_docs(@docs); }


...add it to my Quicksilver path. Hack-tastic.