繁体   English   中英

需要在applescript中返回“ \\”而不是“ \\\\”来构建bash命令

[英]need to return “\” not “\\” in applescript to build bash command

谢谢您的帮助。 我正在尝试使用Applescript构建一系列bash命令。 我的问题是使用\\作为转义字符的applescript。 因此,当它返回我的字符串时,它会根据需要返回\\\\而不是\\ 有没有人找到一种方法来返回带有单个\\的字符串?

另外,我之前在此人上获得的帮助会说“如果您显示对话框,它会说\\ not \\\\ ”,这是正确的,但是我没有尝试显示文本,我需要将这些字符串传递给bash。

--get all the finder info

tell application "Finder"
    set selCnt to selection as list
    if (count of items in selCnt) is equal to 1 then
        set theDir to the selection as alias
        set theDir to POSIX path of theDir
    else if ((count of items in selCnt) is greater than 1) then
        display dialog "Please select only one directory. ie: 'Documents'"
    else if (count of items in selCnt) is equal to 0 then
        set theDir to target of window 1 as alias
        set theDir to POSIX path of theDir
    else
        display dialog "What do you think you're doing?"
    end if

end tell

--rename strings with spaces

set t to theDir
set t to tid(t, " ") -- converts to a list of text items
set t to tid(t, "\\ ") -- converts back to tex a list odf text items
on tid(input, delim)
    set {oldTID, my text item delimiters} to {my text item delimiters, delim}
    if class of input is list then
        set outputA to input as text
    else
        set outputA to text items of input
    end if
    set my text item delimiters to oldTID
    return outputA
end tid
set theDir to result

--strings setup

set cdStr to "cd " & theDir
set stringA to ".doc"
set stringB to "\\"
set delDoc to "find . -name " & quoted form of stringA & " -exec rm {} " & stringB & ":"

set stringA to ".exl"
set stringB to "\\"
set delExl to "find . -name " & quoted form of stringA & " -exec rm {} " & stringB & ":"

--execute shell scripts

set theCmd to cdStr & return & delDoc & return & delExl

return theCmd


(*
do shell script cdStr
do shell script delExl
do shell script delDoc
*)

您只需要用反斜杠转义一个反斜杠来传递该字符串。

set foo to "\\"

将foo设置为单个“ \\”。 您可以使用以下任一方法来验证这一点:

display dialog foo
log foo
return foo
do shell script "echo \\$foo"

运行最后一个命令,它返回“ $ foo”,证明shell命令正确接收了一个斜杠,在这种情况下,该斜杠告诉shell命令忽略$的特殊含义。 如果它向shell命令发送了两个斜杠,那么您将得到一个错误,或者可能是一个文字斜杠。 您可以对编辑器的“ \\”显示感到困惑,以防万一,但是最终结果还是有用的。 您没有在问题中陈述是什么让您认为它的行为不正确。 “吓坏了”是什么意思?

我大多数时候都能使用bash命令弄清楚一切。 然后,我能够创建一个服务,当我按“ cmd + option + R”之类的某个键集时运行。 用户所要做的就是选择文件夹并按下键,程序将擦除所有具有特定扩展名的文件。 我下面的注释中的第2步允许bash识别由Finder的第一个打开的窗口确定的源。

再次为所有格式设置问题而感到抱歉,这只是我在stackoverflow中的第二篇帖子

首先修改.bash_profile。 通过终端打开

  1. 打开〜/ .bash_profile -a TextEdit

将脚本复制并粘贴到txt文件中,然后保存。 现在,您可以发出命令“ cdf”

  1. cdf(){theFinderPath = osascript <<END tell application "Finder" try set thePath to get the POSIX path of (folder of the front window as string) on error set thePath to get the POSIX path of (path to desktop folder as string) end try end tell END cd“ $ theFinderPath”; }

重写脚本以包含新命令,然后将其保存到“文档”文件夹的根目录中。

3。

/ bin / bash

cd到前Finder窗口的路径

cdf查找。 -name'.doc'- exec rm {} \\; #delete查找所有文档文件。 -name'.exe'- exec rm {} \\; #删除所有exe文件

现在,使用Automator,您可以编写一个简单的代码,将通过击键发出此命令。

  1. bash -x /用户/用户名/Documents/sh/script.sh || 真正

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM