Apple Script -> Copy and Paste HTML content - Not working

Discussion in 'Mac Programming' started by gguerini, Apr 10, 2013.

  1. macrumors regular

    gguerini

    Joined:
    Jun 28, 2007
    #1
    Hello guys,

    I love the possibility of sending the content of a webpage using Safari and Mail. The problem is: I don't use Mail. :) I like Sparrow better. So if I try share a page with someone using an email, Sparrow will only receive the link of the page, instead of the content, like Mail would do.

    So I decided to create an Apple Script to do this task.

    This is the idea:
    Get Safari's current tab, go into the Reader view, select all content, copy, create a new email on Sparrow and finally paste the copied content.

    Ok, it's not as smooth as Safari + Mail would do, but it's better then nothing.

    My script works well, except when it pastes the HTML content into Sparrow. If I manually paste (command + v), it works perfectly, but if my script pastes the content, it looses all the formatting. This is my script:

    Code:
    tell application "Safari" to activate
    tell application "System Events"
    	
    	keystroke "r" using {command down, shift down}
    	delay 0.25
    	keystroke "a" using command down
    	delay 0.25
    	keystroke "c" using command down
    	delay 0.25
    	keystroke "r" using {command down, shift down}
    	
    	tell application "Sparrow"
    		activate
    		set theMessage to make new outgoing message with properties {content:get the clipboard}
    		tell theMessage
    			compose
    		end tell
    	end tell
    	
    end tell
    
    What do you think I should do???

    Thanks.
     
  2. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    Try this :

    Code:
    tell application "Safari" to activate
    tell application "System Events"
    	
    	keystroke "r" using {command down, shift down}
    	delay 0.25
    	keystroke "a" using command down
    	delay 0.25
    	keystroke "c" using command down
    	delay 0.25
    	keystroke "r" using {command down, shift down}
    	
    end tell
    
    	tell application "Sparrow" to activate
    		set theMessage to make new outgoing message
    		tell application "System Events"
    		keystroke "v" using command down
    		tell theMessage
    			compose
    		end tell
    		end tell
    	end tell
    
    Note : I don't have Sparrow so this isn't tested.
     
  3. thread starter macrumors regular

    gguerini

    Joined:
    Jun 28, 2007
    #3
    Hey kryten2, you helped me a lot!! :)

    This is the final script:

    Code:
    tell application "Safari"
    	activate
    	set theURL to URL of current tab of window 1
    	set tabName to name of current tab of window 1
    	
    	tell application "System Events"
    		
    		keystroke "r" using {command down, shift down}
    		delay 0.25
    		keystroke "a" using command down
    		delay 0.25
    		keystroke "c" using command down
    		delay 0.25
    		keystroke "r" using {command down, shift down}
    		delay 0.25
    		
    		tell application "Sparrow"
    			activate
    			
    			set theMessage to make new outgoing message with properties {subject:tabName, content:theURL}
    			tell theMessage
    				make new to recipient at end of to recipients with properties {address:""}
    				compose
    				delay 0.5
    				
    				tell application "System Events"
    					keystroke "v" using command down
    				end tell
    			end tell
    			
    		end tell
    		
    	end tell
    end tell
    
    The only thing is I had to add an empty recipient to the message, otherwise it would focus the message body so it would past the content inside the recipient field.

    This is the line:

    Code:
    make new to recipient at end of to recipients with properties {address:""}
    
    But it worked now!! :)

    Thank you so much.

    G

    ----------

    I created an Alfred Workflow to activate the script.

    Open the page you want to e-mail, activate Alfred and type: mailthispage. That's it. It will open your Sparrow and paste the content of the article.

    You can download from here:
    https://www.dropbox.com/sh/1d1repvhtygbke4/HCpuW6MAZe/mailthispage.alfredworkflow
     

Share This Page