是否可以通过编程方式从我的Mac发送短信?

时间:2013-07-25 01:04:57

标签: objective-c macos sms imessage

由于Apple已关闭其开发人员门户网站,等待安全升级,因此他们创建了一个状态页面,以便我们可以监控他们在线恢复的功能。我写了一个简单的程序来监控这个状态页面的变化。

我的Mac设置为接收发送到iPhone的iMessage。我想知道是否有人知道如果有可能让我编写的程序在Apple的状态页面发生变化时向我的iPhone发送iMessage。

我通常为iPhone开发,所以我很欣赏人们可以提供的任何见解。我下面写的简单程序每15分钟检查一次是否有更新,如果有更新,请在Safari上显示更新页面。

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application


    NSDateFormatter *format = [NSDateFormatter new];
    [format setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8*3600]];
    [format setDateFormat:@"YYYY-MM-DD HH:mm:ss"];
    NSString *text = @"";

    bool no_connection;
    do {
        text = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:@"https://developer.apple.com/support/system-status/"] encoding:NSASCIIStringEncoding error:NULL]; // pulls the source html from Apple's update page
        NSString *status = @"No change";
        if ([text rangeOfString:[self currentStatus]].location == NSNotFound) { // if cannot find the old text, then there has been a change
            status = @"Update!";
        }
        no_connection = text == nil || [text length] == 0; // if no text or nil text then connection issue
        if (no_connection) {
            status = @"error making connection";
        }
        NSLog(@"status: %@",status); // report on status

        if (no_connection) { // if no connection then try again in a minute
            sleep(60);
            continue;
        }

        sleep(900); // wait 15 minutes (60 x 15 = 900) and check again

    } while ([text rangeOfString:[self currentStatus]].location != NSNotFound); // continue checking until there has been a change

    NSURL *url = [NSURL URLWithString:@"https://developer.apple.com/support/system-status/"]; // bring up the update page in the browser
    if( ![[NSWorkspace sharedWorkspace] openURL:url] )
        NSLog(@"Failed to open url: %@",[url description]);
}

-(NSString*)currentStatus { /* returns the specific text in the html source that I'm checking for a change
                             "<span>" will be replaced with a hyperlink tag */
    return @"<span>Certificates, Identifiers &amp; Profiles";
}

@end

1 个答案:

答案 0 :(得分:2)

我尝试过一次这样做,从未想出过Objective-C解决方案。但是,您可以让您的应用运行AppleScript。以下是您从命令行执行此操作的方法:

osascript -e 'tell application "Messages" to send "Test Message" to buddy "MyBuddy"'

这里的答案描述了如何从Objective C运行AppleScript:

Run AppleScript from Cocoa Application