void CloseTerminalWindow() { try { AppleScript.Run( @"tell application ""Terminal"" activate set frontmost of {1} to true close {1} tell application ""System Events"" to tell process ""Terminal"" to keystroke return end tell", tabId, windowId); } catch {} }
static void CloseTerminalWindow(string windowId) { try { AppleScript.Run( @"tell application ""{0}"" activate set frontmost of {1} to true close {1} tell application ""System Events"" to tell process ""{0}"" to keystroke return end tell", TERMINAL_APP, windowId); } catch (AppleScriptException) { //it may already have closed } }
// Note: This method may throw TimeoutException or AppleScriptException public void OpenProject(IProgressMonitor monitor) { SyncProject(monitor); monitor.Log.WriteLine("Asking Xcode to open the {0} project...", name); try { AppleScript.Run(XCODE_OPEN_PROJECT, AppleSdkSettings.XcodePath, projectDir); monitor.Log.WriteLine("Xcode successfully opened the {0} project.", name); } catch (AppleScriptException asex) { monitor.Log.WriteLine("Xcode failed to open the {0} project: OSAError {1}: {2}", name, (int)asex.ErrorCode, asex.Message); throw; } catch (TimeoutException) { monitor.Log.WriteLine("Xcode timed out trying to open the {0} project.", name); throw; } }
private void LaunchCommandCallback(string result, Exception ex) { int i = result.IndexOf("of"); tabId = result.Substring(0, i - 1); windowId = result.Substring(i + 3); //rename tab and give it focus AppleScript.Run( @"tell app ""Terminal"" set frontmost of {1} to true set selected of {0} of {1} to true set custom title of {0} of {1} to ""{2}"" activate end tell" , null, tabId, windowId, Escape("MonoTools Terminal")); }
// Note: This method may throw TimeoutException or AppleScriptException public void OpenFile(IProgressMonitor monitor, string relativeName) { SyncProject(monitor); string path = projectDir.Combine(relativeName); monitor.Log.WriteLine("Asking Xcode to open '{0}'...", path); try { AppleScript.Run(XCODE_OPEN_PROJECT_FILE, AppleSdkSettings.XcodePath, xcproj, path); monitor.Log.WriteLine("Xcode successfully opened '{0}'", path); } catch (AppleScriptException asex) { monitor.Log.WriteLine("Xcode failed to open '{0}': OSAError={1}: {2}", path, (int)asex.ErrorCode, asex.Message); throw; } catch (TimeoutException) { monitor.Log.WriteLine("Xcode timed out trying to open '{0}'.", path); throw; } }
public MacExternalConsoleProcess(string command, string arguments, string workingDirectory, IDictionary <string, string> environmentVariables, string title, bool pauseWhenFinished) { //build the sh command var sb = new StringBuilder("clear; "); if (!string.IsNullOrEmpty(workingDirectory)) { sb.AppendFormat("cd \"{0}\"; ", Escape(workingDirectory)); } foreach (string env in environmentVariables.Keys) { string val = environmentVariables[env]; if (!string.IsNullOrEmpty(val)) { val = Escape(val); } sb.AppendFormat("{0}=\"{1}\" ", env, val); } sb.AppendFormat("\"{0}\" {1}", Escape(command), arguments); if (pauseWhenFinished) { sb.Append("; echo; read -p 'Press any key to continue...' -n1"); } sb.Append("; exit"); var cmd = Escape(sb.ToString()); //run the command in Terminal.app and extrac tab and window handles var ret = AppleScript.Run("tell app \"Terminal\" to do script \"{0}\"", cmd); int i = ret.IndexOf("of"); tabId = ret.Substring(0, i - 1); windowId = ret.Substring(i + 3); //rename tab and give it focus AppleScript.Run( @"tell app ""Terminal"" set custom title of {0} of {1} to ""{2}"" set frontmost of {1} to true set selected of {0} of {1} to true activate end tell", tabId, windowId, Escape(title)); }
// Note: This method may throw TimeoutException or AppleScriptException void CloseFile(IProgressMonitor monitor, string fileName) { if (!CheckRunning()) { return; } monitor.Log.WriteLine("Asking Xcode to close '{0}'...", fileName); try { AppleScript.Run(XCODE_CLOSE_IN_PATH, AppleSdkSettings.XcodePath, fileName); monitor.Log.WriteLine("Xcode successfully closed '{0}'", fileName); } catch (AppleScriptException asex) { monitor.Log.WriteLine("Xcode failed to close '{0}': OSAError {1}: {2}", fileName, (int)asex.ErrorCode, asex.Message); throw; } catch (TimeoutException) { monitor.Log.WriteLine("Xcode timed out trying to close '{0}'.", fileName); throw; } }
public void CloseProject() { if (!CheckRunning()) { return; } XC4Debug.Log("Asking Xcode to close the {0} project...", name); try { // Exceptions closing the project are non-fatal. bool closed = AppleScript.Run(XCODE_CLOSE_IN_PATH, AppleSdkSettings.XcodePath, projectDir) == "true"; XC4Debug.Log("Xcode {0} the project.", closed ? "successfully closed" : "failed to close"); } catch (AppleScriptException asex) { XC4Debug.Log("Xcode failed to close the project: OSAError {0}: {1}", (int)asex.ErrorCode, asex.Message); } catch (TimeoutException) { XC4Debug.Log("Xcode timed out trying to close the project."); } }
public void OpenFile(string relativeName) { XC4Debug.Log("Opening file in Xcode: {0}", relativeName); SyncProject(); AppleScript.Run(XCODE_OPEN_PROJECT_FILE, AppleSdkSettings.XcodePath, xcproj, projectDir.Combine(relativeName)); }
public void OpenProject() { SyncProject(); AppleScript.Run(XCODE_OPEN_PROJECT, AppleSdkSettings.XcodePath, xcproj); }
public void SaveProject() { AppleScript.Run(XCODE_SAVE_IN_PATH, AppleSdkSettings.XcodePath, projectDir); }
public void FindMissingTagsAndComments(List <Song> iTunesSongList, Func <Song, bool> compare) { string top100 = "Top 100"; Regex comment = new Regex("(?<year>^[0-9][0-9][0-9][0-9]), #(?<number>[01][0-9][0-9]).*"); Regex badComment = new Regex("^[0-9][0-9][0-9][0-9], #[01][0-9][0-9] [0-9][0-9][0-9][0-9], #[0-9]?[1-9].*"); var timer = Top100Timer.Start("FindMissingTagsAndComments"); foreach (Song dbSong in dbSongsList.List.FindAll(x => x.Own.Equals(true) && compare(x))) { var list = iTunesSongList.FindAll(x => x.IsMatch(dbSong)); if ((list != null) && (list.Count >= 1)) { foreach (Song s in list) { bool updateSong = false; string appleScript = "tell application \"iTunes\"\n" + " activate\n" + String.Format(" set results to (every file track of playlist \"Library\" whose name contains (\"{0}\") and artist contains (\"{1}\"))\n", scrubString(s.Title), scrubString(s.Artist)) + " repeat with t in results\n"; if (!s.Grouping.Contains(top100)) { appleScript += String.Format(" set t's grouping to \"{0}\" as text\n", addTag(s.Grouping, top100)); Top100Util.Debug(String.Format("Missing Grouping: {0}=>{1}", s, addTag(s.Grouping, top100))); updateSong = true; } if (badComment.IsMatch(s.Comments)) { appleScript += String.Format(" set t's comment to \"{0}\" as text\n", prependComment(s.Comments, dbSong.Year, dbSong.Number)); Top100Util.Debug(String.Format("Bad Comment: {0}=>{1}", s, prependComment(s.Comments, dbSong.Year, dbSong.Number))); updateSong = true; } else if (!comment.IsMatch(s.Comments)) { appleScript += String.Format(" set t's comment to \"{0}\" as text\n", prependComment(s.Comments, dbSong.Year, dbSong.Number)); Top100Util.Debug(String.Format("Missing Comment: {0}=>{1}", s, prependComment(s.Comments, dbSong.Year, dbSong.Number))); updateSong = true; } else if (comment.IsMatch(s.Comments)) { if (dbSong.Number < s.Number) { appleScript += String.Format(" set t's comment to \"{0}\" as text\n", prependComment(s.Comments, dbSong.Year, dbSong.Number)); Top100Util.Debug(String.Format("Updating Comment: {0}=>{1}", s, prependComment(s.Comments, dbSong.Year, dbSong.Number))); updateSong = true; } } if (updateSong) { appleScript += " end repeat\n" + "end tell\n"; try { if (!Top100Settings.Preview) { AppleScript.Run(appleScript); } } catch (Exception e) { Top100Util.Error(String.Format("Cannot update song: {0}\n\tException: {1}\n\tUsing: {2}", s, e, appleScript)); } } } } else { Top100Util.Error("Cannot find owned song in Library. " + dbSong); } } timer.End(); }
// Note: This method may throw TimeoutException or AppleScriptException public void SaveProject(IProgressMonitor monitor) { monitor.Log.WriteLine("Asking Xcode to save pending changes for the {0} project", name); AppleScript.Run(XCODE_SAVE_IN_PATH, AppleSdkSettings.XcodePath, projectDir); }
static bool TabExists(string tabId, string windowId) { return(AppleScript.Run("tell app \"Terminal\" to get exists of {0} of {1}", tabId, windowId) == "true"); }
internal static Task <int> RunTerminal( string command, string arguments, string workingDirectory, IDictionary <string, string> environmentVariables, string title, bool pauseWhenFinished, out string tabId, out string windowId, CancellationToken cancelToken = default(CancellationToken)) { TaskCompletionSource <int> taskSource = new TaskCompletionSource <int> (); cancelToken.Register(delegate { taskSource.SetCanceled(); }); //build the sh command var sb = new StringBuilder("clear; "); if (!string.IsNullOrEmpty(workingDirectory)) { sb.AppendFormat("cd \"{0}\"; ", Escape(workingDirectory)); } if (environmentVariables != null) { foreach (string env in environmentVariables.Keys) { string val = environmentVariables [env]; if (!string.IsNullOrEmpty(val)) { val = Escape(val); } sb.AppendFormat("export {0}=\"{1}\"; ", env, val); } } if (command != null) { sb.AppendFormat("\"{0}\" {1}", Escape(command), arguments); var tempFileName = Path.GetTempFileName(); sb.Append($"; echo $? > {tempFileName}"); var fileWatcher = new FileSystemWatcher(Path.GetDirectoryName(tempFileName), Path.GetFileName(tempFileName)); fileWatcher.EnableRaisingEvents = true; fileWatcher.Changed += delegate { lock (taskSource) { if (taskSource.Task.IsCompleted) { return; } taskSource.SetResult(int.Parse(File.ReadAllText(tempFileName))); File.Delete(tempFileName); } }; if (pauseWhenFinished) { sb.Append("; echo; read -p \"Press any key to continue...\" -n1"); } sb.Append("; exit"); } //run the command in Terminal.app and extract tab and window IDs string appleScript; if (string.IsNullOrEmpty(command)) { appleScript = string.Format("tell app \"{0}\" to do script \"{1}\"", TERMINAL_APP, Escape(sb.ToString())); } else { // run the command inside Bash because we do echo $? and that is a bash extension and breaks when people // use other shells such as zsh or fish. https://bugzilla.xamarin.com/show_bug.cgi?id=56053 appleScript = string.Format("tell app \"{0}\" to do script \"bash -c '{1}'; exit\"", TERMINAL_APP, Escape(sb.ToString())); } var ret = AppleScript.Run(appleScript); int i = ret.IndexOf("of", StringComparison.Ordinal); tabId = ret.Substring(0, i - 1); windowId = ret.Substring(i + 3); //rename tab and give it focus sb.Clear(); sb.AppendFormat("tell app \"{0}\"\n", TERMINAL_APP); if (!string.IsNullOrEmpty(title)) { sb.AppendFormat("\tset custom title of {0} of {1} to \"{2}\"\n", tabId, windowId, Escape(title)); } sb.AppendFormat("\tset frontmost of {0} to true\n", windowId); sb.AppendFormat("\tset selected of {0} of {1} to true\n", tabId, windowId); sb.Append("\tactivate\n"); sb.Append("end tell"); try { AppleScript.Run(sb.ToString()); } catch (Exception ex) { taskSource.SetException(ex); } return(taskSource.Task); }
private void KillWhenDone(object state) { AppleScript.Run("tell app \"Terminal\" to get processes of {0} of {1}", KillCallback, tabId, windowId); }
static bool TabExists(string tabId, string windowId) { return(AppleScript.Run("tell app \"{0}\" to get exists of {1} of {2}", TERMINAL_APP, tabId, windowId) == "true"); }