from foremost in safari to foremost finder: folder with webarchive&txt

Thanks for helping me finish my first “big” applescript guys!
I will probably change this script in the future. When I do I’ll post it here.
Special thanks to StefanK that quickly ending some searches for solutions that don’t exist and regulus6633 at stackoverflow that wrote large parts of this.

A rough description of how this works:

You have a safari window open.
You open a finder folder. (Using spotlight, launchbar or whatever.)
The script will make a new folder in the foremost finder window later.
Launch this script. (I use keyboard maestro for this.)
The script displays the title of the safari window, edit it as you see fit.
Then it makes a folder in the foremost finder window given the name of the title edited by you.
It then saves a copy in that folder (webarchive or from source depending on what’s the default.)
It makes and opens a txt, with the edited title as a headline as well as the original title, url and date/time.

The script still has some of my random notes. Maybe I’ll take them out later.

--this script will be launched by a hotkey
tell application "Safari" to set {theSource, theTitle, theURL} to {source, name, URL} of document 1



tell application "Safari" to set {theSource, theTitle, theURL} to {source, name, URL} of document 1

tell application "Finder"
	display dialog "newName_dialogue" default answer theTitle
	set newName to (text returned of result)
	set selectedFinderItem to (folder of the front window) as text
	make new folder at selectedFinderItem with properties {name:newName}
	set pathOfNewFolder to selectedFinderItem & newName as text
	duplicate file "Q:x:7:n7:GTD scripting:template folder:x.txt" to pathOfNewFolder as string
	set pathOfX to pathOfNewFolder & ":x.txt" as string
	set name of file pathOfX to (newName as text) & ".txt"
	set pathOfNewTXT to pathOfNewFolder & ":" & newName & ".txt"
	--	"display dialog pathOfNewTXT" was useful as a test, it made it clear you forgot to add ":"
	
	
end tell
--get lot's of different versions of the document to the MCOTWS folder:
tell application "Safari"
	activate
	delay 0.2
	--Get posix path of target file
	set targetFile to pathOfNewFolder
	set pOSIXPathOfTargetFile to POSIX path of targetFile
	--display dialog pOSIXPathOfTargetFile
	
	--Tell activated safari to find and "click save" & Tell safari to activate
	tell application "Safari"
		activate
		delay 0.1
		tell application "System Events"
			tell process "Safari"
				--this will only save stuff in one place
				click menu item "Save As." of menu 1 of menu bar item "File" of menu bar 1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke (ASCII character 31) --> down arrow
				delay 0.1
				keystroke "pa"
				delay 0.1
				keystroke return
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke pOSIXPathOfTargetFile
				key down return
				key up return
				delay 0.2
				key down return
				key up return
			end tell
			tell process "Safari"
				--this will only save stuff in one place
				click menu item "Save As." of menu 1 of menu bar item "File" of menu bar 1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke (ASCII character 31) --> down arrow
				delay 0.1
				keystroke "we"
				delay 0.1
				keystroke return
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke pOSIXPathOfTargetFile
				key down return
				key up return
				delay 0.2
				key down return
				key up return
			end tell
		end tell
	end tell
	
	--I'm going to put some stuff that gets the old thing saved
	--pathOfNewFolder, i'll have to get the posix of this ... 
	--do stefan ks script
end tell
--I'll also make a new folder in which I put

set templateFile to "Q:x:7:n7:GTD scripting:template folder:SI_Script.txt"
set copyFolder to "Q:X:7:SI:SIAG1:" -- notice this path ends in ":" because it's a folder

--something to write safari docs to the folder and a subfolder
--something to write url, and timestamp to the txt

-- get the text of the template file
set templateText to read file templateFile

-- add the file name in CAPS, a colon, and 2 returns at the beginning of templateText
set capsName to upperCase(newName)
set newText to capsName & ":" & return & return & templateText & return & return & "URL: " & theURL & return & "Original title: " & theTitle & return & "Download date: " & (current date) as text


-- write the newText to pathOfNewTXT
writeTo(pathOfNewTXT, newText, text, false)

-- open the new file in textedit
tell application "TextEdit" to open file pathOfNewTXT
tell application "TextEdit" to activate
delay 0.2
tell application "System Events" to keystroke (ASCII character 31) --> down arrow
tell application "System Events" to keystroke (ASCII character 31) --> down arrow



(*============== SUBROUTINES (BEG) ==============*)
on writeTo(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as text
		if targetFile does not contain ":" then set targetFile to POSIX file targetFile as text
		set openFile to open for access file targetFile with write permission
		if apendData is false then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end writeTo

on upperCase(theText)
	set chrIDs to id of theText
	set a to {}
	repeat with i from 1 to (count of chrIDs)
		set chrID to item i of chrIDs
		if chrID ≥ 97 and chrID ≤ 122 then set chrID to (chrID - 32)
		set end of a to chrID
	end repeat
	return string id a
end upperCase
(*============== SUBROUTINES (END) ==============*)