Tuesday, November 25, 2014

#1 2012-05-11 02:07:11 pm

JohnOfChocobo
Member
Registered: 2012-05-11
Posts: 7

Trouble "merging" AppleScripts

Hey Guys,

I'll be honest, I'm a near total AppleScript newb. I've been trying to learn AppleScript through a number of small scripting projects that i was hoping to collate into one larger script that would be expandable to my personal needs.

I have 2 scripts that i would like to merge, one that posts to twitter using Twurl and the other that allows me to send messages from my iPhone to my mac and my mac will a reply back (with a filter to check if the message was from me).

I found the basic skeletons for both scripts on the internet then modified them to my needs (I did try to write my own full versions first but couldn't do it without a basic skeleton to give me hints at how the commands interact)

My problem is being able to merge the scripts together, so an off shoot of my twitter script can be packed into an if statement in my other script, so if I say "Tweet Hello Twitter!" it will check for the word "Tweet" and execute a command to remove the "Tweet " part of my message and then post the rest to twitter. the problem is, every time i try and add the twitter script to my message script the Applescript editor refuses to compile the code due to myself being unable to use the correct syntax.

I've tried looking for information on the syntax but I couldnt find a remedy to my problem. If someone could explain where I am going wrong that would be a big help, as I have many little scripts that i have written since and would like to add to this one messaging script.

Both Scripts work fine independently, though i have removed some of the twitter script here (dialog boxes etc, as in this scenario i wouldnt need them)

here is the code i have managed to bundle together:

Applescript:



property Myself : {" "} -- This property handles all my personal information for my message filter. This includes my name, phone number and email address. (removed for privacy reasons)

--Twitter Details
property theAccount : " " -- this is where I enter my twitter acc name.
property theApp : " " -- the name of my twitter "app" that i registered with twitter so i could use twurl

using terms from application "iChat"
   on message received theMessage from theBuddy for theChat
       -- if the message is from myself
       if (handle of theBuddy) is in Myself then
           -- then send the message on to be processed by the if statement
           check(theMessage, theBuddy, theChat)
       end if
   end message received
end using terms from

--process messages received in ichat
on check(theMessage, theBuddy, theChat)
   tell application "iChat"
       
       -- if the message is hello
       if theMessage contains "Hello jarvis" or theMessage contains "Hi jarvis" then
           -- then return the greeting
           try
               send ("Hi there, " & ((name of theBuddy) as string)) to theChat
           end try
           return
       end if
       
       --heres where things go wrong :/
       
       if theMessage contains "Tweet " then
           try
               set theMessage to tweet's replaceText("Tweet", "", theMessage) -- this following snippet *should* remove the "Tweet " part of my original message to just leave the Tweet to be sent.
               
               replaceText(find, replace, someTweet)
               set prevTIDs to text item delimiters of AppleScript
               set text item delimiters of AppleScript to find
               set someTweet to text items of someTweet
               set text item delimiters of AppleScript to replace
               set someTweet to "" & someTweet
               set text item delimiters of AppleScript to prevTIDs
               set AppleScript's text item delimiters to ""
               return someTweet as string --found this snippet on the internet, I don't know if it works though.
               
-- heres where I pasted my script (ive tried using try, run, no command here, everything ive seen before in scripts and tutorials that could work >.<
   
   -- This tells twurl what account to use
   set theResult to (do shell script "twurl set default " & theAccount)
   
   set theTweet to someTweet
   
-- Checking tweet length due to the restriction
on checkLength(theMessage)
   if length of theMessage is less than or equal to 140 then
       postTweet(theTweet)
   end if
end checkLength

-- Posting to Twitter API
on postTweet(theTweet)
   set theContent to quoted form of ("source=" & theApp & "&status=" & theTweet)
   set TwitterUpdate to "twurl -d " & theContent & " /1/statuses/update.xml"
   set TwitterResponse to do shell script TwitterUpdate
   
end postTweet
               
           on error msg
               return "Error: " & msg
           end try
           
       end if
           
   end tell
end check

If anyone could tell me where I am going wrong or any code optimizations I would be forever grateful!

John


"We drank! We fought! He made his ancestors proud!" - Thor

Filed under: syntax, Merging Scripts

Offline

 

#2 2012-05-11 02:33:34 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 1567

Re: Trouble "merging" AppleScripts

I can't give any kind of guaranty but I guess that the reorganized script is approaching what you tried to achieve.

Applescript:



property Myself : {" "} -- This property handles all my personal information for my message filter. This includes my name, phone number and email address. (removed for privacy reasons)

--Twitter Details
property theAccount : " " -- this is where I enter my twitter acc name.
property theApp : " " -- the name of my twitter "app" that i registered with twitter so i could use twurl

using terms from application "iChat"
   on message received theMessage from theBuddy for theChat
       -- if the message is from myself
       if (handle of theBuddy) is in Myself then
           -- then send the message on to be processed by the if statement
           my check(theMessage, theBuddy, theChat)
       end if
   end message received
end using terms from

--process messages received in ichat
on check(the_Message, the_Buddy, the_Chat)
   tell application "iChat"
       
       -- if the message is hello
       if the_Message contains "Hello jarvis" or the_Message contains "Hi jarvis" then
           -- then return the greeting
           try
               send ("Hi there, " & (name of the_Buddy)) to the_Chat
           end try
           return
       end if
       
       --heres where things go wrong :/
       
       if the_Message contains "Tweet " then
           try
               set theTweet to my replaceText("Tweet", "", the_Message)
               
               -- heres where I pasted my script (ive tried using try, run, no command here, everything ive seen before in scripts and tutorials that could work >.<
               
               -- This tells twurl what account to use
               set theResult to (do shell script "twurl set default " & theAccount)
               (* What to do with theResult *)
           on error msg
               return "Error: " & msg
           end try
           
       end if
       
   end tell
end check


(* this handler isn’t used *)
-- Checking tweet length due to the restriction
on checkLength(theMessage)
   if length of theMessage is less than or equal to 140 then
       postTweet(theTweet)
   end if
end checkLength
(* So this one is no more used *)
-- Posting to Twitter API
on postTweet(theTweet)
   set theContent to quoted form of ("source=" & theApp & "&status=" & theTweet)
   set TwitterUpdate to "twurl -d " & theContent & " /1/statuses/update.xml"
   set TwitterResponse to do shell script TwitterUpdate
end postTweet

-- this following snippet *should* remove the "Tweet " part of my original message to just leave the Tweet to be sent.
on replaceText(find, replace, someTweet)
   set prevTIDs to text item delimiters of AppleScript
   set text item delimiters of AppleScript to find
   set someTweet to text items of someTweet
   set text item delimiters of AppleScript to replace
   set someTweet to (someTweet as text)
   set text item delimiters of AppleScript to prevTIDs
   --set AppleScript's text item delimiters to ""
   return someTweet --found this snippet on the internet, I don't know if it works though.
end replaceText

Yvan KOENIG (VALLAURIS, France) vendredi 11 mai 2012 21:33:25

Offline

 

#3 2012-05-14 03:52:41 pm

JohnOfChocobo
Member
Registered: 2012-05-11
Posts: 7

Re: Trouble "merging" AppleScripts

Thanks! I managed to fix the problem and learnt a bit about the syntax, the "on" command is a separate subroutine, any subroutines should be put at the bottom of the script to be "Called", I changed this and the script works fine now!


"We drank! We fought! He made his ancestors proud!" - Thor

Offline

 

#4 2012-05-15 02:12:37 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 1567

Re: Trouble "merging" AppleScripts

The handlers aren’t forced to be at the end of the script.
I'm acustomed to work this way but some other authors put them at the beginning. wink


There is an alternate syntax replacing « on » by « to ».

Yvan KOENIG (VALLAURIS, France) mardi 15 mai 2012 09:12:30

Last edited by Yvan Koenig (2012-05-15 02:13:31 am)

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.033 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)