gikoha’s blog

個人的メモがわり

ASOC

AppleScriptStudioがXcode4.2で消えていたので、しょうがなくASOCを学びはじめた
が、これまた情報がほとんどない
しょうがねーなー

以下はURLを解析して、画像っぽいのをダウンロードするソフト。

.xibには progress indicator, button, fieldがあってそれぞれoutletと接続されている
と考えてくだされ。


逆にDownloadボタンからのActionはApp DelegateのClickButton:に繋がっている

perlerなので当然内容はperlでゴリゴリ書きます。ASOCはあくまでフロントエンド。

script AppDelegate
	property parent : class "NSObject"
    property button : missing value
    property field : missing value
    property progress : missing value

	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened 
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
    on clickButton_(sender)
        button's setEnabled_(false)
        progress's setIndeterminate_(true)
        progress's setUsesThreadedAnimation_(true)
        progress's startAnimation_(me)
        progress's displayIfNeeded()
        
        set aFolder to choose folder with prompt "ダウンロード先"
        set aDirectory to POSIX path of aFolder

        set CWD to POSIX path of (path to resource "analyze.pl")
        set CWD2 to POSIX path of (path to resource "download.pl")

        set aurl to field's |stringValue|()
        set theURL to (aurl as string)

        set theScript to "cd '" & aDirectory & "'; /usr/bin/perl " & CWD & " " & theURL
        set kekka to do shell script theScript

        set the AppleScript's text item delimiters to return
        set theList to text items of kekka
        set maxnum to count text items of kekka
        
        set the AppleScript's text item delimiters to ""
        set num to 0

        progress's setMinValue_(0)
        progress's setMaxValue_(maxnum)
        progress's setIndeterminate_(false)
        
        repeat with i in theList
            progress's setDoubleValue_(num)
            progress's displayIfNeeded()
           set theScript to "cd '" & aDirectory & "'; /usr/bin/perl " & CWD2  & " " & (contents of i) & " " & (num as string)
            -- display dialog theScript
            do shell script theScript
            set num to num+1
        end repeat

        progress's setIndeterminate_(true)
        progress's stopAnimation_(me)
        button's setEnabled_(true)

    end clickButton_
    
end script

analyze.pl

$arg1 = $ARGV[0];
$tempfile = '/tmp/temp.html';
system 'curl',  '--user-agent', '"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)"', '--silent', '--referer', $arg1, $arg1, '-o', $tempfile;

open (IN, $tempfile);
binmode(IN);
sysread(IN,$buf,2);
close(IN);
@data = unpack("C*", $buf);

if($data[0]==0x1F && $data[1]==0x8B) {      # gzipped
    rename($tempfile,$tempfile . ".gz");
    system 'gunzip', $tempfile . ".gz";
}
else
{
}

# convert to EUC to Unicode without BOM, overwrite file in place

system '/usr/local/bin/nkf', '-E', '-w80', '--in-place', $tempfile; 

# analyze link

open (IN, $tempfile);

LINE: while(<IN>) {
	next if !(/画像リンクスタート条件/);  # ここかえとけやー
    
LINE2: while (<IN>) {
        if(/画像リンクエンド条件/)  # ここかえとけやー
        {	close(IN);
            exit 0;
        }
        chomp;
        $x = $_;
LINEX:
        $ansp = index($x,'a href="'); # ここかえとけやー  a href=""の中を抜き出すだけ
        next if($ansp<0);
        $ans = substr($x,$ansp+8); # ここかえとけやー
        
        $ans2p = index($ans, '"');
        next if($ans2p<0);
        $ans2 = substr($ans,0,$ans2p);
        $x = substr($ans,$ans2p+1);
        
        goto LINEX if index($ans2, '.html')>0;
        goto LINEX if index($ans2, '.php')>0;
        goto LINEX if index($ans2, '.zip')>0;
        
        print $ans2 . "\n";
        goto LINEX;
    }
}

download.pl

use File::Basename;

$arg1 = $ARGV[0];
$arg2 = $ARGV[1];
($name,$path,$suffix)=fileparse($arg1, qr/\.[^.]*/);
$filename = sprintf "file%03d%s",$arg2,$suffix;

system 'curl',  '--user-agent', '"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)"', '--silent', '--referer', $arg1, $arg1, '-o', $filename;