Run AppleScript from Cocoa Application

Solved! Xcode wasn’t saving my script file into app’s resources path. To run an AppleScript code from Cocoa Application, use this: NSString *path = [[NSBundle mainBundle] pathForResource:@”ScriptName” ofType:@”scpt”]; NSURL *url = [NSURL fileURLWithPath:path];NSDictionary *errors = [NSDictionary dictionary]; NSAppleScript *appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors]; [appleScript executeAndReturnError:nil]; Swift 5.6.1: import AppleScriptObjC import Cocoa let path = … Read more

Change OSX keyboard layout(“input source”) programmatically via terminal or AppleScript?

You can do it using the Text Input Services API: NSArray* sources = CFBridgingRelease(TISCreateInputSourceList((__bridge CFDictionaryRef)@{ (__bridge NSString*)kTISPropertyInputSourceID : @”com.apple.keylayout.French” }, FALSE)); TISInputSourceRef source = (__bridge TISInputSourceRef)sources[0]; OSStatus status = TISSelectInputSource(source); if (status != noErr) /* handle error */; The dictionary in the first line can use other properties for other criteria for picking an input … Read more

OSX: check if the screen is locked

First, there’s a bit of confusion in your question. Both Shift+Control+Eject and Energy Saver put the screens to sleep, which isn’t the same thing as locking them. Depending on your other settings, this may also entail locking the screen, but that’s a separate issue. IIRC, on Lion, by default, neither one will ever lock the … Read more

Avoiding AppleScript through Ruby: rb-appscript or rubyosa?

Quoth kch: That’s nice, but now I’m curious about how scripting bridge compares to applescript. I guess I’ll have some reading to do. SB omits some functionality found in AppleScript. For example, the following script moves all files from the desktop to the Documents folder: tell application “Finder” move every file of desktop to folder … Read more

osascript using bash variable with a space

You could try to use instead : osascript -e “display notification \”$var2\”” Or : osascript -e ‘display notification “‘”$var2″‘”‘ This fixes the problem of manipulation of variables that contains spaces in bash. However, this solution doesn’t protect against injections of osascript code. So it would be better to choose one of Charles Duffy’s solutions or … Read more

is there something akin to regEx in applescript, and if not, what’s the alternative?

Don’t despair, since OSX you can also access sed and grep through “do shell script”. So: set thecommandstring to “echo \”” & filename & “\”|sed \”s/[0-9]\\{10\\}/*good*(&)/\”” as string set sedResult to do shell script thecommandstring set isgood to sedResult starts with “*good*” My sed skills aren’t too crash hot, so there might be a more … Read more

Applescript; opening an app in Space number N

In OS X 10.5 or 10.6, Spaces assignments can be accessed and changed via the scriptable interface to System Events.app: tell application “System Events” set x to application bindings of spaces preferences of expose preferences set x to {|com.apple.textedit|:4} & x — Have TextEdit appear in space 4 set application bindings of spaces preferences of … Read more