applescript - AppleScript 在应用程序保存窗口中不起作用
问题描述
我有一个在 finder 中完美运行的 AppleScript。它会创建一个以日期命名的文件夹,并有一个与之连接的快捷键。
但是在另一个应用程序中,在保存窗口中它不起作用。你能帮忙吗?
这是我的代码。
tell application "Finder"
try
if exists Finder window 1 then
set thisPath to (the target of the front window) as alias
else
set thisPath to (path to desktop)
end if
on error
return
end try
end tell
set x to my the_perfect_datestring()
if x is not "-ERROR" then
set fullPath to thisPath & x as text
tell application "Finder"
try
--activate
if not (exists fullPath) then
set y to make new folder at thisPath with properties {name:x}
end if
activate
end try
end tell
end if
on the_perfect_datestring()
try
set cd to (the current date)
set the_year to year of (cd) as number
set the_month to month of (cd) as number
set the_day to day of (cd) as number
if the_month < 10 then set the_month to "0" & the_month
if the_day < 10 then set the_day to "0" & the_day
return ((the_year & "-" & the_month & "-" & the_day) as string)
on error
return "-ERROR"
end try
解决方案
要在其他应用程序中使用保存对话框,您需要稍微更改您的工作流程。保存对话框使用文本(文件/文件夹名称),因此您可以创建文本服务以粘贴所需的字符串。
要创建服务,请启动 Automator 应用程序并选择服务(快速操作)文档 - 工作流将具有确定它将接受何种输入的设置,例如:
Workflow receives current text in any application
接下来,选中该Output replaces selected text
框 - 这将使用工作流的输出替换选定的文本。
将Run AppleScript操作拖到工作流中,并用新的处理程序和您的日期字符串处理程序完全替换默认内容Run
,例如:
on run {input, parameters}
set x to the_perfect_datestring()
if x is not "-ERROR" then return x
end run
on the_perfect_datestring()
try
# do your filename stuff
tell (current date) as «class isot» as string
return text 1 thru 10
end tell -- or whatever
on error
return "-ERROR"
end try
end the_perfect_datestring
保存工作流后,只要您选择一些文本(在右键单击上下文菜单中),该服务就应该可用。
然后,与您使用 Finder 的原始工作流程等效的是创建一个新文件夹,并使用突出显示的“未命名文件夹”名称上的服务来更改它。
推荐阅读
- java - 从 inputStream 中读取字节的 Int 值
- security - 使用 packer vmware-vmx 并配置不支持带密码的 ssh 的机器
- java - 列出 16x16x16 矩阵内可能的最大长方体
- ios - 如何使用按钮和图像选择多个单元格以选中和取消选中
- sql-server - SQL Server 库存年龄计算 - 嵌套游标的替代方法
- scipy - 如何在 Python 中实现 FIR 高通滤波器?
- linux - 如何在 linux 中组装、链接和运行 .s 文件?
- javascript - 网页无限滚动时如何让Apify爬虫滚动整页?
- azure - 在 Datafactory Azure 上的 SSIS 2016 中将变量父级传递给子级的问题
- tomcat8 - 如何通过允许多个 IP 范围而不是特定 IP 地址来限制对我的 tomcat 页面的访问?