What wrong with this script?

Discussion in 'Mac Programming' started by thriftinkid, Apr 25, 2008.

  1. thriftinkid macrumors regular

    Joined:
    Mar 24, 2008
    #1
    Hey Guys,

    I've been messing with a script I found that send out multiple emails to different people. The following script should read the email addresses I want to send the my message to from a .txt file called "address.txt" I got it to work through automator on one system with MAC OS 10.5. But I tested it on another system with 10.4 and I got the following error:

    Applescript Error:

    Can't make into type file.


    Any ideas?

    Here is the script, including the automator end tags:

    Code:
    on run {input, parameters}
    	
    	--direct to your plain text file with addresses, and name, tab delimited
    	set theAddressDOC to read file
    	("MacintoshHD:Users:test:Movies:address.txt")
    	
    	set pcount to count paragraphs in theAddressDOC
    	repeat with i from 1 to number of paragraphs in theAddressDOC
    		set this_item to paragraph i of theAddressDOC
    		
    		set Name_text to "" -- set to blank
    		-- using try so if there is a an error, in this case it will be when the is no name, the script will carry on
    		-- instead of stopping. The Name_text will reamin as "" if it does
    		try
    			--use delimiters (tab) in this case to split the result of this_item and try to set Name_text to the name
    			set AppleScript's text item delimiters to tab
    			set para_text to text of this_item
    			--get the second half of paragraph
    			set Name_text to text item 2 of para_text
    		end try
    		--get the first half of paragraph
    		set address_text to text item 1 of para_text
    		
    		--reset the delimiters
    		set AppleScript's text item delimiters to ""
    		
    		set theAddress to address_text
    		
    		-- direct to a doc of what you want the email to say in plain text
    		set theletter to file
    		("MacintoshHD:Users:test:Movies:web links body text.txt")
    		
    		-- What is the subject of the Emails
    		set theSubject to "Here Are Your Requested Uploaded Links"
    		
    		-- Check to see if Name_text contains a name or not
    		if Name_text is "" then
    			--if no name
    			set theBody to " you to view" & theletter
    			
    		else
    			--the must be a name
    			set theBody to "Here are the links for " & Name_text & theletter
    		end if
    		theBody
    		
    		
    		-- Choose the account to send the message from
    		set theSender to "myemail@gmail.com"
    		
    		tell application "Mail"
    			
    			set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
    			tell newMessage
    				-- Default is false. Determines whether the compose window will
    				-- show on the screen or whether it will happen in the background.
    				set visible to true
    				set sender to theSender
    				make new to recipient at end of to recipients with properties {address:theAddress}
    				tell content
    					tell application "Mail"
    						send newMessage
    					end tell
    					-- delay to allow the Mail program to send and not bog down, adjust as needed
    					delay 10
    				end tell
    			end tell
    		end tell
    		
    	end repeat
    	return input
    end run
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Add some:
    Code:
    display dialog "Message here"
    In there and narrow down where the error is happening. Once you know what line it is, if you still can't figure it out post again. Make each message different so you can tell exactly where in the script it's running into trouble.

    I'm pretty sure there's a way to just put messages into the "console" in the script editor, but this should work fine for narrowing down where this is happening.

    -Lee
     
  3. thriftinkid thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #3
    It is showing it can't read the bolded part of the following line:

    set theAddressDOC to read file
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    I moved the ("MacintoshHD:Users:test:Movies:address.txt") onto the same line that had the error and it seemed to work properly.

    -Lee
     
  5. Sayer macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #5
    There are two problems

    1. You have not defined the variable "file" before trying to use it
    2. You cannot use "file" as a variable name as it is a reserved keyword

    Any of this not make sense to you?

    How about doing:

    set sourceFile to "MyHD:path:To:File.txt"

    and then ... read sourceFile ?

    You could even do at the beginning of the script: property sourceFile : "MyHD:path:To:File.txt"

    This has the benefit of being editable from within/by the script and the changes saved within the script file automatically.
     
  6. thriftinkid thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #6
    I tried setting the source file firstr, but I got the following syntax error:

    Expected end of line, etc. but found “(”.
     
  7. thriftinkid thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #7
    I tried to re write it like the following:

    Code:
    set sourceFile to "MacintoshHD:Users:test:Movies:address.txt"
    	set theAddressDOC to read sourceFile
    and I got this applescript error:

    Can't make "MacintoshHD:Users:test:Movies:address.txt" into type file.


    I then re wrote it again like this:


    Code:
    set sourceFile to "MacintoshHD:Users:test:Movies:address.txt"
    	set theAddressDOC to sourceFile
    It ran the script. But when I looked at the out going mail message. Instead of applying the text within the text document "address.txt", it literally added the text documents location "MacintoshHD:Users:test:Movies:address.txt" into the header.
     
  8. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #8
    Code:
    set sourceFile to open for access "MacintoshHD:Users:test:Movies:address.txt"
    set theAddressDOC to read sourceFile 
    close access sourceFile
    This of course, assumes your file is there. You might be better off browsing for it.
     
  9. thriftinkid thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #9
    I gave it a shot, but it started adding crazy amount of info, and I have no idea where it is pulling it from:

    {\colortbl;\red255\green255\blue255;}\margl1440\margr1440\vieww9000\viewh8400\viewkind0

    It kept opening up new mail messages and adding more similar text. None of this is the text from the "address.txt" document.
     
  10. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #10
    You sure that's a text file you are dealing with? and not an RTF saved with a .txt file extension?
     
  11. thriftinkid thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #11
    I don't believe so. Is there a way to make absolutely sure?
     
  12. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #12
    I think it is.

    Open it in a real text editor, such as TextWrangler, TextMate, or vi and inspect the contents.

    Or simply type the following in Terminal, and inspect the result.

    Code:
    cat /Users/test/Movies/address.txt
    Just because something opens (or is created) with TextEdit, it doesn't mean it's a text file.
     
  13. thriftinkid thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #13
    the first response line from the terminal is:

    {\rtf1\mac\ansicpg10000\cocoartf824\cocoasubrtf470

    I'm assuming that would mean its an RTF. What is the difference? And can I changed the current coding, or do I have to go back and change the coding where that text document is actually created to save out differently?
     
  14. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #14
    I don't use TextEdit, but I would assume there is an option in the Save As dialog to make it plain text - which is what you want.
     
  15. thriftinkid thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #15
    I checked before, and it didn't give me an option. I thought I bypassed it by specifying in my script how I wanted it saved. My other issue is that I already had this script working running with Mac OS 10.5. I am trying to put it on a system with 10.4, and now I'm getting these issues. Do you know why?
     
  16. thriftinkid thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #16
    I changed my preferences to automatically save to a plain text doc always, but it didn't help
     

Share This Page