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

查看:213
本文介绍了在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 ..."块.我的问题是,当我尝试进行编译时,它告诉我它期望一个结束",但发现一个接通".我假设它要我在在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天全站免登陆