Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 29, 2006, 02:14 PM   #1
dolphin842
macrumors 65816
 
dolphin842's Avatar
 
Join Date: Jul 2004
Applescript for saving webpage in Safari

Hi all,

A friend and I are making a simple AppleScript for automatically saving the current webpage in Safari. The twist is that the filename for the saved webpage is based on whatever text the user selects in the webpage (i.e. the title of a news article). Here is what we have so far:

Code:
tell application "Safari"
	activate
	set the_url to URL of document 1
end tell

tell application "System Events"
	tell process "Safari"
		click menu item "Copy" of menu "Edit" of menu bar 1
	end tell
end tell

set the_filename to the clipboard
set the_path to "'/Users/Roy/Documents/" & the_filename & ".html'"

do shell script "curl -o " & the_path & " '" & the_url & "'"
After a fresh restart of Safari, the first run of this script works perfectly. If I go to a second webpage, highlight different text, and run the script again, the file generated from the previous run gets overwritten by the second webpage, and no file with the selected text from the second webpage is made. If I go to a third webpage, select different text, then run, a new file with the selected text from the *second* webpage is created with the data from the *third* webpage. This behavior indicates that the curl command is going according to plan, but something with the clipboard is getting messed up. Any help would be much appreciated!

EDIT: Nevermind folks.... putting a delay between the copy command and setting the_filename to the clipboard did the trick!

EDIT2: fwiw, we ended up using the save function in Safari directly... this produces an html file with a ".html.download" extension, so a shell script takes it back to ".html". I've got the script mapped to a function key and it's quite useful if you only save to a few directories regularly. Here's the final version:

Code:
tell application "Safari"
	activate
end tell

tell application "System Events"
	tell process "Safari"
		click menu item "Copy" of menu "Edit" of menu bar 1
	end tell
end tell

delay 1

set the_filename to (the clipboard) & ".html"
set the_filepath to "Macintosh HD:Users:Roy:Documents:" & the_filename
set the_shellfilepath to "'/Users/Roy/Documents/" & the_filename & ".download'"
set the_shellfilepath2 to "'/Users/Roy/Documents/" & the_filename & "'"

tell application "Safari"
	activate
	save document 1 in the_filepath
end tell

do shell script "mv " & the_shellfilepath & " " & the_shellfilepath2

Last edited by dolphin842; Nov 29, 2006 at 03:11 PM. Reason: figured it out
dolphin842 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
What am I doing wrong (issues with saving a webpage)? 420benz iMac 4 Apr 15, 2014 01:57 PM
AppleScript: Problem Saving Files in the correct place mrnohands2004 Mac Programming 0 Mar 2, 2013 07:16 AM
Safari webpage snippets firmansolutions Mac Applications and Mac App Store 2 Nov 4, 2012 10:40 AM
cannot see the comment section of a webpage on safari please help! supertoto2 Mac Applications and Mac App Store 10 Aug 28, 2012 11:39 AM

Forum Jump

All times are GMT -5. The time now is 05:56 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC