- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个要在Terminal.app中运行的窗口/选项卡,分别是“syd”和“mel”。即在Shell中|列出了“新窗口”,“syd”和“mel”。如何使用AppleScript打开这些终端配置?
最佳答案
几周前,我从Tiger(10.4)机器上迁移了新的Snow Leopard(10.6)机器。 Tiger的终端将其设置存储在“.term”文件中(通常存储在~/Library/Application Support/Terminal/
中,但可以将其保存/移动到任何位置); Snow Leopard的终端将其设置集中到其首选项文件中。
在迁移到Snow Leopard之前,我正常的工作流程之一是使用Finder双击已保存的“.term”文件,以预设大小和初始命令打开“终端”窗口。今天,我注意到,每次执行此终端操作时,都会创建一个重复的“设置集”。因此,我开始寻找一种方法来启动保存的设置,该方法不涉及打开“.term”文件(这样就不会堆积重复的设置)。自从我有过使用AppleScript的经验以来,AppleScript是我的第一站。
简而言之,似乎没有直接的方法可以通过终端的AppleScript命令启动具有特定“设置”的新窗口/标签。通常的方法是做一些涉及make new window
(或make new tab
)的事情,但是我找不到Terminal可以接受的变体。我提出了三种替代解决方案(我认为最后一种是最好的)。
创建一个窗口,然后更改设置
如果您的设置不涉及初始命令或与默认设置不同的大小(例如,只有颜色/键盘/行为设置与默认设置不同),则可以使用终端的do script
命令(不带“text”参数)来创建新的新窗口,然后将其settings set
更改为您想要的那个。
tell application "Terminal"
set newTab to do script -- create a new window with no initial command
set current settings of newTab to settings set "Grass"
end tell
default settings
default settings
属性。我认为可以暂时更改默认设置,创建一个新窗口,然后重置默认设置。这种方法最终成功了,但结果却很丑陋(除了临时更改为默认值的丑陋之外)。
keystroke
命令将⌘N发送到终端以创建新窗口。事实证明,Terminal创建新窗口有时会有点慢,并且在Terminal有机会使用脚本较早部分安排的临时值之前,我的脚本最终将重置为默认值。
do script
本来是同步的,但也会使保存为设置一部分的任何初始命令无效。我最终求助于⌘N之前的窗口数,然后等待窗口数增加。如果启动的命令导致窗口的快速打开和关闭,则该循环可能会卡住。我可以限制迭代次数,但是到那时,我对代码的整体风格感到非常失望(尽管我确实继续进行并扩展它以允许使用新的选项卡,而不仅仅是Windows)。
to newTerminal(settingSetName, asTab)
tell application "Terminal"
if asTab is true then
set countRef to a reference to tabs of first window
set newKey to "t"
else
set countRef to a reference to windows
set newKey to "n"
end if
set originalDefault to name of default settings
set default settings to settings set settingSetName
end tell
try
set initialCount to count of countRef
tell application "System Events"
-- keystrokes always go to the frontmost application
set frontmost of application process "Terminal" to true
keystroke newKey using command down
end tell
repeat until (count of countRef) > initialCount
beep
delay 0.1
end repeat
tell application "Terminal" to set default settings to settings set originalDefault
on error m number n
try
tell application "Terminal" to set default settings to settings set originalDefault
end try
error m number n
end try
end newTerminal
newTerminal("Grass", false)
(* makeNewTerminal(settingsSetName, asTab)
Bring Terminal.app to the front and
click the menu item named <settingsSetName>.
If <asTab> is true, then use the menu item under Shell > New Tab,
otherwise use the menu item under Shell > New Window
*)
to makeNewTerminal(settingsSetName, asTab)
tell application "Terminal" to launch
if asTab is true then
set submenuName to "New Tab"
else
set submenuName to "New Window"
end if
tell application "System Events"
set terminal to application process "Terminal"
set frontmost of terminal to true
click menu item settingsSetName of ¬
first menu of menu item submenuName of ¬
first menu of menu bar item "Shell" of ¬
first menu bar of terminal
end tell
end makeNewTerminal
makeNewTerminal("Grass", true)
关于macos - AppleScript打开命名的终端窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1794050/
还有人在用applescript吗,苹果把文档存档了,好久没更新语言了。这是否意味着语言已死? 最佳答案 自从苹果在 2016 年解散 Mac 自动化团队并(迟迟)解雇了负责它的产品经理以来,整个 A
我无法弄清楚如何检测 AppleScript 中按下的键以及如何延迟直到该键被释放。我想切换缩放,我拥有其他一切(我认为)。这是我当前的代码 on idle set ztoggle to 0
是否可以为使用 AppleScript 创建的应用程序保存某种设置? 设置应在脚本开始时加载,并在脚本结束时保存。 例子: if loadSetting("timesRun") then se
我正在尝试显示我选择的文件的扩展名。我做错了什么? set theFile to (choose file with prompt "Select a file to transfer:")
我正在尝试制作一个将在后台运行并且每周只在特定时间执行一次的 applescript,有什么建议吗? 最佳答案 您可能想要的是 idle handler .首次打开 AppleScript 应用程序时
我希望能够引用与 Controller 位于同一目录中的模型文件。 最初,它们位于项目的根文件夹中,但当它们被编译(使用 osacompile)时,它们都将位于 Controller.scptd/Co
我不知道如何使用 AppleScript,一开始我是如何让我的小 bash 脚本工作的,这超出了我的能力。我目前正在使用 AppleScript 来运行我的 bash 脚本,它工作得非常好。它在下面。
我有一系列 AppleScript 命令需要在一行 AppleScript 中实现。代码如下。 delay 2 tell application "System Events" to keystrok
在 VBA 中,用不同的子字符串替换子字符串非常容易,例如 objMsg.Body = Replace(objMsg.Body, ""在电子邮件正文中使用当前月份。我以前见过与此类似的问题,但它们已经
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我已被任命创建一个必须 当网页加载完后,显示一个对话框,显示"The webpage has finished loading." 确定当前正在下载的项目数量 对于数字1,我尝试做if the U
我有一个用于创建作业文件夹的 Applescript。 Applescript 要求提供项目名称和文件夹的存储位置。输入作业名称和文件夹位置后,脚本会创建主文件夹和四个子文件夹。 现在我希望脚本在当前
我有一个字符串,其中包含要删除的非法字符,但我不知道可能存在哪种字符。 我建立了一个我不希望被过滤的字符列表,并建立了这个脚本(来自我在网络上找到的另一个脚本)。 on clean_string(Th
我很困惑 - 我已经用谷歌搜索了一个小时,并尝试了大约十种不同形式的 set posixDirectory to POSIX path of (parent of (path to aFile) as
我在 Automator 中有一个小的 applescript: do shell script "osascript ~/Focus-On.scpt" delay 60 do shell scrip
我想打开一个默认为应用程序设置文件夹的文件夹: /Users/XXX/Library/Application Support/Tunnelblick/Configurations 我不想“硬编码”“X
我试图找出最前面的应用程序 x 是什么,运行脚本,然后使用 applescript 将 x 设置为最前面的应用程序。 tell application "System Events" set
有一个 AppleScript 方法: on displayError(theErrorMessage) display dialog theErrorMessage return "
我正在尝试编写一个脚本来记录当前应用程序、切换到另一个应用程序、执行一些任务并返回到原始应用程序。这就是我所拥有的 set currentApp to my getCurrentApp() activ
我正在尝试根据其文本使用 AppleScript 选择大纲的特定行。 这是我正在考虑的(但不起作用): repeat with aRow in rows of outline 1 of scroll
我是一名优秀的程序员,十分优秀!