Need some help with AppleScript

Discussion in 'Mac Programming' started by davidlt, Dec 30, 2007.

  1. davidlt macrumors member

    Joined:
    May 22, 2007
    Location:
    Lithuania
    #1
    Code:
    -- kintamieji
    set theStatusCheck to false
    set theSMSCount to 0
    set theSMSTotalCount to 0
    set theFailCount to 0
    
    -- parametrai
    set theFailLimit to 20
    set theTimeOutTime to 40
    set theLogFile to (path to desktop as Unicode text) & "log.txt"
    set theRecipient to "863711111"
    set theSMSText to " "
    
    -- kodas
    
    -- į Log failą įrašo duomenis
    on insertToLog(log_item)
    	global theLogFile
    	set fRef to (open for access file theLogFile with write permission)
    	try
    		write log_item as string to fRef starting at eof
    	end try
    	close access fRef
    end insertToLog
    
    
    
    insertToLog("
    
    --- Skriptas pradeda darbą. (Laikas: " & ((current date) as string) & ", Parametrai: {FailLimit: " & theFailLimit & ", TimeOutTime: " & theTimeOutTime & ", Recipient: " & theRecipient & ", SMSText: " & theSMSText & "})")
    
    tell application "BluePhoneElite 2"
    	repeat while theStatusCheck = false
    		with timeout of theTimeOutTime seconds
    			try
    				set theSMSCount to theSMSCount + 1
    				send message theSMSText to theRecipient ¬
    					using the first device whose available is true
    				set theFailCount to 0
    			on error errStr number errorNumber
    				set theSMSCount to theSMSCount - 1
    				insertToLog("
    -- Klaida (Laikas: " & (time string of (current date)) & "). " & errStr & " (" & (errorNumber as number) & "). Duomenys: {SMSCount: " & theSMSCount & ", SMSTotalCount: " & theSMSTotalCount & ", FailCount: " & theFailCount & "}")
    				set theFailCount to theFailCount + 1
    				if theFailCount > theFailLimit then
    					set theStatusCheck to true
    					set theSMSTotalCount to theSMSTotalCount + theSMSCount - 1
    					insertToLog("
    -- Skriptas baigė darbą." & ((current date) as string) & " Išsiųsta žinučių: " & theSMSTotalCount & ". Duomenys: {SMSCount: " & theSMSCount & ", FailCount: " & theFailCount & "}")
    					display dialog "Klaida! Daugiau negaliu siųti SMS. Išsiunčiau: " & theSMSTotalCount
    				end if
    				set theSMSTotalCount to theSMSTotalCount + theSMSCount
    				set theSMSCount to 0
    			end try
    		end timeout
    	end repeat
    end tell
    This is my small script which is using BluePhoneElite 2 to send SMS, massive amount of SMS. I am new to AppleScript and I have three questions.

    1. set theLogFile to (path to desktop as Unicode text) & "log.txt" <- this line holds the path to the log file, which is in desktop, but I would like that that log file to be in the same folder as my script. I tested many ways, but script can't find it. How should I write it correctly?

    2. Inside tell block I can't access my insertToLog let's call it "function". Why? Is there any way which would allow be to do this. Or I should make some kind of object and do like this: tell Logger to insertNewItem "blabal.. "

    3. Sometimes I get AppleScript errors and always it is the same error about timeout. This way I am using with timeout of ... seconds, but can I catch those errors and handle them too? I don't want my script to break on this kind of errors.

    And the last question would be about the documentation. I found a few websites with articles about AppleScript and official Apple documentation, but it looks a bit poor for me. There is a lot missing, like how to work with files, write/read data and etc. Is there more detailed manual/tutorial or even a book that I could read?

    Thanks for the help. It's pretty amazing how easy is to read those scripts...
     
  2. tjwett macrumors 68000

    tjwett

    Joined:
    May 6, 2002
    Location:
    Brooklyn, NYC
    #2
    1. if you are dealing with file/folders you need to first reference them from the Finder:

    tell app "Finder"
    set theLogFile to "path:to:file.txt"

    i'm in a hurry at the moment but if you post this to the forums at macscripter.net you'll have your answer in minutes.
     

Share This Page