Search in sources :

Example 1 with NSAppleScript

use of ch.cyberduck.binding.foundation.NSAppleScript in project cyberduck by iterate-ch.

the class PreferencesController method configureProxiesButtonClicked.

@Action
public void configureProxiesButtonClicked(NSButton sender) {
    final String script = "tell application \"System Preferences\"\n" + "activate\n" + "reveal anchor \"Proxies\" of pane \"com.apple.preference.network\"\n" + "end tell";
    NSAppleScript open = NSAppleScript.createWithSource(script);
    open.executeAndReturnError(null);
}
Also used : NSAppleScript(ch.cyberduck.binding.foundation.NSAppleScript) NSMutableAttributedString(ch.cyberduck.binding.foundation.NSMutableAttributedString) NSAttributedString(ch.cyberduck.binding.foundation.NSAttributedString) Action(ch.cyberduck.binding.Action) WindowMainAction(ch.cyberduck.core.threading.WindowMainAction) DefaultMainAction(ch.cyberduck.core.threading.DefaultMainAction) TransferAction(ch.cyberduck.core.transfer.TransferAction)

Example 2 with NSAppleScript

use of ch.cyberduck.binding.foundation.NSAppleScript in project cyberduck by iterate-ch.

the class ApplescriptTerminalService method open.

@Override
public void open(final Host host, final Path workdir) throws AccessDeniedException {
    final boolean identity = host.getCredentials().isPublicKeyAuthentication();
    final Application application;
    switch(StringUtils.lowerCase(finder.find(".command").getIdentifier())) {
        case "com.googlecode.iterm2":
        case "com.apple.terminal":
            application = finder.find(".command");
            break;
        default:
            log.warn(String.format("Unsupported application %s assigned", finder.find(".command")));
            application = finder.getDescription(preferences.getProperty("terminal.bundle.identifier"));
    }
    if (!finder.isInstalled(application)) {
        throw new LocalAccessDeniedException("Unable to determine default Terminal application");
    }
    String ssh = MessageFormat.format(preferences.getProperty("terminal.command.ssh"), identity ? String.format("-i \"%s\"", host.getCredentials().getIdentity().getAbsolute()) : StringUtils.EMPTY, host.getCredentials().getUsername(), host.getHostname(), String.valueOf(host.getPort()), this.escape(workdir.getAbsolute()));
    if (log.isInfoEnabled()) {
        log.info(String.format("Execute SSH command %s", ssh));
    }
    // Escape
    ssh = StringUtils.replace(ssh, "\\", "\\\\");
    // Escape all " for do script command
    ssh = StringUtils.replace(ssh, "\"", "\\\"");
    if (log.isInfoEnabled()) {
        log.info("Escaped SSH Command for Applescript:" + ssh);
    }
    // Applescript
    final String applescript;
    switch(application.getIdentifier()) {
        case "com.googlecode.iterm2":
            applescript = MessageFormat.format(preferences.getProperty("terminal.command.iterm2"), ssh);
            break;
        default:
            applescript = MessageFormat.format(preferences.getProperty("terminal.command.default"), ssh);
            break;
    }
    final String command = "tell application \"" + application.getName() + "\"" + "\n" + "activate" + "\n" + applescript + "\n" + "end tell";
    if (log.isInfoEnabled()) {
        log.info(String.format("Execute AppleScript %s", command));
    }
    final NSAppleScript as = NSAppleScript.createWithSource(command);
    final ObjCObjectByReference error = new ObjCObjectByReference();
    if (null == as.executeAndReturnError(error)) {
        final NSDictionary d = error.getValueAs(NSDictionary.class);
        throw new LocalAccessDeniedException(String.format("Failure running script in %s. %s", application.getName(), d.objectForKey("NSAppleScriptErrorBriefMessage")));
    }
}
Also used : NSDictionary(ch.cyberduck.binding.foundation.NSDictionary) NSAppleScript(ch.cyberduck.binding.foundation.NSAppleScript) ObjCObjectByReference(org.rococoa.ObjCObjectByReference) Application(ch.cyberduck.core.local.Application) LocalAccessDeniedException(ch.cyberduck.core.exception.LocalAccessDeniedException)

Aggregations

NSAppleScript (ch.cyberduck.binding.foundation.NSAppleScript)2 Action (ch.cyberduck.binding.Action)1 NSAttributedString (ch.cyberduck.binding.foundation.NSAttributedString)1 NSDictionary (ch.cyberduck.binding.foundation.NSDictionary)1 NSMutableAttributedString (ch.cyberduck.binding.foundation.NSMutableAttributedString)1 LocalAccessDeniedException (ch.cyberduck.core.exception.LocalAccessDeniedException)1 Application (ch.cyberduck.core.local.Application)1 DefaultMainAction (ch.cyberduck.core.threading.DefaultMainAction)1 WindowMainAction (ch.cyberduck.core.threading.WindowMainAction)1 TransferAction (ch.cyberduck.core.transfer.TransferAction)1 ObjCObjectByReference (org.rococoa.ObjCObjectByReference)1