在 AppleScript 中查找和替换 [英] Find and Replace in AppleScript

查看:23
本文介绍了在 AppleScript 中查找和替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个超级简单的脚本,我需要在变量中查找和替换文本(特别是在放置到 applescript 上的项目的路径中.)这是我目前所拥有的:

I'm currently writing a super simple script and I need to find and replace text in a variable (specifically in the path of the items dropped onto the applescript.) Here is what I have currently:

    on open {dropped_items}
    tell application "Finder" to set filePathLong to the POSIX path of dropped_items as text


on replaceText(find, replace, subject)
    set prevTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to find
    set subject to text items of subject

    set text item delimiters of AppleScript to replace
    set subject to "" & subject
    set text item delimiters of AppleScript to prevTIDs

    return subject
end replaceText

get replaceText("/mpc/mayors1", "/ifs/disk1", filePathLong)




display dialog subject

end open

(排除不相关的代码并添加一个对话框来验证它是否有效)

(excluding irrelevant code and adding a dialog to verify it worked)

我从 Stack Overflow 搜索这篇文章的标题中得到的on replaceText..."块.我的问题是,当我尝试编译时,它告诉我它期望结束"但发现打开".我假设它希望我在on replaceText"之前结束我的开放,但我不想那样做.关于我可以做些什么来让它工作的任何想法?对不起,如果这很简单,我对 AppleScript 还很陌生.

That "on replaceText..." block I got from searching Stack Overflow for the title of this post. My problem is that when I try to compile it tells me it expected an "end" but found an "on." I'm assuming it wants me to end my open before I can "on replaceText" but I don't want to do that. Any ideas as to what I could do to get it to work? Sorry if this is very simple, I'm pretty new to AppleScript.

我知道我可以去掉前十二个字符,然后在字符串的开头添加/ifs/disk1",但我想知道为什么这不起作用,以防再次发生这种情况.

I understand I could just chop off the first twelve characters and then add "/ifs/disk1" to the beginning of the string but I want to know why this isn't working in case this happens again.

推荐答案

您不能将处理程序放置在另一个(显式)处理程序中.还做了一些其他更正.

You can't place a handler within another (explicit) handler. Made some other corrections as well.

on open dropped_items
    repeat with anItem in dropped_items
        set filePathLong to anItem's POSIX path
        set mySubject to replaceText("/mpc/mayors1", "/ifs/disk1", filePathLong)
        display dialog mySubject
    end repeat
end open


on replaceText(find, replace, subject)
    set prevTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to find
    set subject to text items of subject

    set text item delimiters of AppleScript to replace
    set subject to subject as text
    set text item delimiters of AppleScript to prevTIDs

    return subject
end replaceText

这篇关于在 AppleScript 中查找和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆