Jump to content

Recommended Posts

(Sorry I didn't complete my title and can't alter it)

 

Hopping someone can help me out.

I'm wanting to copy a URL from Safari and paste it into a new TextEdit document and then save it to a specific location.

 

So far I can get it as far as my clipboard then I get stuck, by using an AppleScript:

 

tell application "Safari"

  activate
          set theURL to URL of front document
  set the clipboard to theURL & return
end tell
Edited by harrietb
Link to comment

You don't need the clipboard for this. You can pass the URL straight to TextEdit. This shows you how to create and save TextEdit documents:
 

-- Called when script is executed
on run (argv)
	-- Base name of the document without directory or file extension
	-- e.g. "Important Stuff"
	set documentName to "My Document"
	-- Where to save new files. Default is ~/Documents
	set saveDirectory to (path to documents folder)
	-- Make sure this is something TextEdit understands, e.g. ".rtf", ".txt"
	set fileExtension to ".rtf"
	set theText to my getSafariUrl()
	set thePath to my newFilePath(documentName, saveDirectory, fileExtension)
	createTextEditDocument(thePath, theText)
end run


-- Create a new document containing `theText` and save it at `thePath`
on createTextEditDocument(thePath, theText)
	
	tell application "TextEdit"
		activate
		make new document with properties {text:theText}
		save document 1 in file thePath
	end tell
	
end createTextEditDocument


-- Return the URL of Safari's current tab
on getSafariUrl()
	
	tell application "Safari" to return URL of front document
	
end getSafariUrl


-- Return an unused filepath by appending 1,2,3 etc. to `documentName`
on newFilePath(documentName, saveDirectory, fileExtension)
	
	set theSuffix to 1
	
	tell application "Finder"
		
		set fileName to (documentName as text) & fileExtension
		set thePath to (saveDirectory as text) & fileName
		
		repeat while item (thePath) exists
			set fileName to (documentName as text) & " " & (theSuffix as text) & fileExtension
			set thePath to (saveDirectory as text) & fileName
			set theSuffix to theSuffix + 1
		end repeat
		
	end tell
	
	return thePath
	
end newFilePath

Link to comment

 

You don't need the clipboard for this. You can pass the URL straight to TextEdit. This shows you how to create and save TextEdit documents:

 

-- Called when script is executed
on run (argv)
	-- Base name of the document without directory or file extension
	-- e.g. "Important Stuff"
	set documentName to "My Document"
	-- Where to save new files. Default is ~/Documents
	set saveDirectory to (path to documents folder)
	-- Make sure this is something TextEdit understands, e.g. ".rtf", ".txt"
	set fileExtension to ".rtf"
	set theText to my getSafariUrl()
	set thePath to my newFilePath(documentName, saveDirectory, fileExtension)
	createTextEditDocument(thePath, theText)
end run


-- Create a new document containing `theText` and save it at `thePath`
on createTextEditDocument(thePath, theText)
	
	tell application "TextEdit"
		activate
		make new document with properties {text:theText}
		save document 1 in file thePath
	end tell
	
end createTextEditDocument


-- Return the URL of Safari's current tab
on getSafariUrl()
	
	tell application "Safari" to return URL of front document
	
end getSafariUrl


-- Return an unused filepath by appending 1,2,3 etc. to `documentName`
on newFilePath(documentName, saveDirectory, fileExtension)
	
	set theSuffix to 1
	
	tell application "Finder"
		
		set fileName to (documentName as text) & fileExtension
		set thePath to (saveDirectory as text) & fileName
		
		repeat while item (thePath) exists
			set fileName to (documentName as text) & " " & (theSuffix as text) & fileExtension
			set thePath to (saveDirectory as text) & fileName
			set theSuffix to theSuffix + 1
		end repeat
		
	end tell
	
	return thePath
	
end newFilePath

This is a life saver.

Appreciate this! Going to get start on learning all this.

Cheers!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...