lib/imessage/sender.rb in imessage-0.3.1 vs lib/imessage/sender.rb in imessage-0.3.2

- old
+ new

@@ -1,15 +1,13 @@ +require 'shellwords' + module Imessage class Sender - def deliver(options = {text:nil, attachment:nil, contacts: []}) - if options[:text].nil? && options[:attachment].nil? - raise "You must specific at least a text or attachment." - end + def deliver(options = { text: nil, attachment: nil, contacts: [] }) + raise 'You must specific at least a text or attachment.' if options[:text].nil? && options[:attachment].nil? - if options[:contacts].empty? - raise "You must specific at least one contact" - end + raise 'You must specific at least one contact' if options[:contacts].empty? options[:contacts].each do |contact| _deliver(options[:text], options[:attachment], contact) end end @@ -30,38 +28,27 @@ deliver_text(text, contact) deliver_attachment(attachment, contact) end def deliver_attachment(attachment, contact) - script = <<-SCRIPT - on run argv - set toAddress to first item of argv - set theFilePath to second item of argv - set theFile to POSIX file theFilePath - tell application "System Events" - if exists file theFilePath then - tell application "Messages" - send theFile to buddy toAddress of (service 1 whose service type is iMessage) - end tell - else - error "File not exist." - end if - end tell - end run - SCRIPT - `osascript -e'#{script}' '#{contact}' '#{attachment}'` + apple_script_file = File.join(File.dirname(File.expand_path(__FILE__)), 'scripts/send_attachment.applescript') + + cmd = <<~CMD.strip + osascript #{apple_script_file} "#{contact}" "#{attachment}" + CMD + + system cmd end def deliver_text(text, contact) - script = <<-SCRIPT - on run argv - set toAddress to first item of argv - set message to second item of argv - tell application "Messages" - send message to buddy toAddress of (service 1 whose service type is iMessage) - end tell - end run - SCRIPT - `osascript -e '#{script}' '#{contact}' '#{text}'` + apple_script_file = File.join(File.dirname(File.expand_path(__FILE__)), 'apple_scripts/send_text.applescript') + + cmd = <<~CMD.strip + osascript #{apple_script_file} "#{contact}" "#{text}" + CMD + + puts cmd + + system cmd end end end