V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
qian
V2EX  ›  问与答

怎么在一个 AppleScript 里套用其他 AppleScript 的 handler/function/subroutine/method?

  •  
  •   qian · 2014-06-17 20:34:22 +08:00 · 2415 次点击
    这是一个创建于 3607 天前的主题,其中的信息可能已经有所发展或是发生改变。
    第 1 条附言  ·  2014-06-17 22:05:56 +08:00
    Solved.
    4 条回复    2014-06-17 22:46:44 +08:00
    orzfly
        1
    orzfly  
       2014-06-17 22:35:13 +08:00
    推荐把你的解决方法也发一下方便后人……
    qian
        2
    qian  
    OP
       2014-06-17 22:44:42 +08:00
    @orzfly stackoverflow 那位仁兄已经帮忙解决了。额,我这边贴一下我稍微改动的 solution :

    snippet 1

    on removeText(unwanted, theText)
    set {TID, text item delimiters} to {text item delimiters, unwanted}
    set theText to text items of theText
    set text item delimiters of AppleScript to ""
    set theText to theText as text
    set text item delimiters to TID
    return theText
    end removeText

    on replaceText(unwanted, wanted, theText)
    set {TID, text item delimiters} to {text item delimiters, unwanted}
    set theText to text items of theText
    set text item delimiters of AppleScript to wanted
    set theText to theText as text
    set text item delimiters to TID
    return theText
    end replaceText

    把 snippet 1 保存到 Script Libraries (我没看懂 http://macosxautomation.com/mavericks/libraries/index.html 里在说什么,总之不算了我就在 ~/Library 新建名为“Script Libraries”的文件夹,把 text.scpt 放在里面)

    以后有需复用 AS text manipulation snippet 的情况,我就把 snippet 扔 text.scpt 里。

    实际使用肯定离不开 TextExpander (TE),在 TE 新建一个 AS 文件夹,再新建若干 rules:

    ASreplace

    tell script "text" to set theText to replaceText("%filltext:name=old%", "%filltext:name=new%", theText)

    ASremove

    tell script "text" to set theText to removeText("old", theText)

    在 Hazel 用 AS 利用文件名和 AS text item delimiters 进行文件/文件夹 filing 时,有 TE 就方便许多。
    qian
        3
    qian  
    OP
       2014-06-17 22:45:32 +08:00
    不算了 -> 不管了 (...
    qian
        4
    qian  
    OP
       2014-06-17 22:46:44 +08:00
    ...

    ASremove

    tell script "text" to set theText to removeText("%filltext:name=old%", theText)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   771 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 20:26 · PVG 04:26 · LAX 13:26 · JFK 16:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.