首页 > 解决方案 > 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

标签: applescript

解决方案


要在其他应用程序中使用保存对话框,您需要稍微更改您的工作流程。保存对话框使用文本(文件/文件夹名称),因此您可以创建文本服务以粘贴所需的字符串。

要创建服务,请启动 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 的原始工作流程等效的是创建一个新文件夹,并使用突出显示的“未命名文件夹”名称上的服务来更改它。


推荐阅读