AppleScript mit Safari Object PDF speichern

J

jabbawock

Registriert
Thread Starter
Dabei seit
06.01.2011
Beiträge
2
Reaktionspunkte
0
Hallo zusammen,

ich bin neu hier und auch recht neu mit AppleScript unterwegs, habe auch im Forum gesucht bin aber leider nicht fündig geworden.

Ich würde gerne ein PDF von einer Webseite speichern. Da ich mich auf diese Webseite einloggen muss und ein Cookie gesetzt wird etc. funktioniert das Abspeichern mit "URL Access Scripting" nicht. Zumindest ist das meine Theorie ;)

Gibt es eine Funktion/Objekt mit Safari mit dem ich das machen kann und wie muss der Pfad aussehen...

So mache ich es bis jetzt und es funktioniert alles, bis auf das abspeichern.
Code:
set mainURL to "<Webseite>" as text
set mainLogin to "<User>" as text
set mainPword to "<PWD>" as text


tell application "Safari"
	activate
	set thisDocument to make new document at end of documents
	-- open MainWindow
	do JavaScript "window.open('" & mainURL & "')" in thisDocument
	
	tell application "System Events"
                -- login
		tell application process "Safari"
			delay 5
			-- keystroke (ASCII character 9) — tab character
			
			keystroke (ASCII character 9) -- tab character
			keystroke mainLogin
			--delay 1
			keystroke (ASCII character 9) -- tab character
			--delay 1
			keystroke mainPword
			keystroke (ASCII character 13) -- this equals return
			
			delay 5
		end tell
	end tell
	-- open stupid iFrames
	set sDownloadseite to do JavaScript "parent.ErstesiFrame.Zweitesiframe.document.links[0].href" in front document
	do JavaScript "window.open('" & sDownloadseite & "')" in front document
	delay 3
        
-- get PDF Url
	set sPdfUrl to do JavaScript "document.links[0].href" in front document
	
	delay 3
	-- get PDF Name
	set AppleScript's text item delimiters to "/"
	set sFileName to last text item of (sPdfUrl as text)
	set AppleScript's text item delimiters to ""
	--- save PDF
	tell application "URL Access Scripting"
		download sPdfUrl to "/Users/username/" & sFileName replacing yes
	end tell
end tell
Wäre euch für eine Hilfe sehr dankbar.
 
Ok habe das Problem jetzt wie folgt gelöst. Vielleicht hat ja noch jemand eine bessere Idee... habe lange Delays drin, da das PDF riesig ist und es eh um 5 Uhr morgens auf meinem Server läuft wo es keinen stört...

Code:
set mainURL to "<URL>" as text
set mainLogin to "<User>" as text
set mainPword to "<PWD>!" as text
set sFilepath to "/Users/name/folder/" as text

--Safari starten
tell application "Safari"
	activate
	
	--Handelsblattseite aufrufen
	set thisDocument to make new document at end of documents
	do JavaScript "window.open('" & mainURL & "')" in thisDocument
	
	--einloggen
	tell application "System Events"
		tell application process "Safari"
			delay 10
			-- keystroke (ASCII character 9) — tab character
			
			keystroke (ASCII character 9) -- tab character
			keystroke mainLogin
			delay 5
			keystroke (ASCII character 9) -- tab character
			delay 5
			keystroke mainPword
			keystroke (ASCII character 13) -- this equals return
		end tell
	end tell
	--warten bis Seite geladen dann dämliche iFrames laden
	delay 10
	set sDownloadseite to do JavaScript "parent.content.cn.document.links[2].href" in front document
	--display dialog sDownloadseite
	do JavaScript "window.open('" & sDownloadseite & "')" in front document
	
	-- warten bis Seite geladen dann PDF aufrufen
	delay 10
	set sPDF to do JavaScript "document.links[0].href" in front document
	do JavaScript "window.open('" & sPDF & "')" in front document
	
	
	-- warten bis PDF geladen...gähn
	delay 100
	
	
	-- Filenamen extrahieren
	-- set AppleScript's text item delimiters to "/"
	-- set sFileName to last text item of (sPDF as text)
	-- set AppleScript's text item delimiters to ""
	-- display dialog sFileName
	
	
	tell application "System Events" to tell process "Safari"
		-- Save as aufrufen
		keystroke "s" using {command down, shift down}
		delay 10
		
		-- Ordner fedtlegen und speichern
		tell window "Save As"
			keystroke "g" using {command down, shift down}
			delay 2
			tell sheet 1
				keystroke sFilepath
				keystroke (ASCII character 13)
			end tell
			keystroke (ASCII character 13)
		end tell
	end tell
	-- Safari beenden
	quit
end tell
 
Zurück
Oben Unten