读取文本文件的内容,并根据文件扩展名将内容设置为变量

时间:2014-05-22 20:44:08

标签: applescript

首先,我不是Scripter。我正在寻找一种方法来访问特定文件,TextFile(通过其扩展名)并读取其内容并将这些内容设置为AppleScript中的变量(为简单起见,假设TextFile不需要解析)。关键问题是特定文件位于不同的机器(本地服务器)上。你怎么能访问这些文件;此脚本必须能够在连接到网络(Windows计算机/服务器)的任何MAC计算机上运行。

我之前的方法是将pathVariable设置为" x.x.x.xx / Share / Apps / CodeBase / test.txt" AppleScript抛出错误声称它"无法生成文件"

不确定为什么会出现此错误,因为如果我将x.x.x.xx(ip)路径更改为Volumes / share / ....它会运行,但是我需要IP,原因是之前在帖子中解决的原因。

我在网上和StackOverflow上看到的例子都没有提供足够的结果,我也没有在AppleScripts上找到任何质量教程(所以任何好的资源都会受到赞赏)。

修改

由于某种原因,我的ExecuteScript()函数永远不会被调用,程序就会退出。任何解释都会很好

执行脚本功能

on ExecuteScript()
tell application "Terminal"
// Do Stuff
end tell
end ExecuteScript

结束ExecuteScript功能

ReadFile功能

on readFile(unixPath)

set foo to (open for access (POSIX file unixPath))

set txt to (read foo for (get eof foo))

close access foo

return txt

end readFile

结束ReadFile功能

调用函数的代码

delay 0.3
try
set diskName to "Share"
-- need logic to check if Volume is already mounted
if diskName is in (do shell script "/bin/ls /Volumes") then

    set contentsOfFile to readFile(filePath)
    ExecuteScript()
else
    -- mount
    mount volume "smb://x.x.xx/Share"
    set contentsOfFile to readFile(filePath)
    ExecuteScript()
end if

end try

2 个答案:

答案 0 :(得分:0)

服务器是Mac吗?如果是这样,您可以打开"远程Apple事件"在“共享”首选项窗格中。然后你可以将applescript命令直接发送到那台机器上,任务很简单。

如果它不是Mac,那么您的下一个最佳选择是首先在本地挂载文件服务器,然后从挂载的磁盘读取该文件。所有这些都可以通过一点点努力在AppleScript中完成。

第三种方法是使用诸如rsync或scp之类的unix工具将文件从服务器复制到本地计算机,然后将其读入AppleScript。使用这种方法,您可以在每台计算机上设置SSH密钥,这样您就可以从Applecript运行命令而无需提供密码。如果没有SSH密钥,您将不得不在终端中发出命令。

因此,如果没有关于服务器的更多信息,很难提出最佳解决方案。

关于您的错误我不理解,因为以下内容对我来说没有错误。你必须做其他导致错误的事情。

set pathVariable to "x.x.x.xx/Share/Apps/CodeBase/test.txt"

答案 1 :(得分:0)

经过数小时的研究和试验以及错误,我找到了解决方案。

解决服务器位置问题并通过代理解决文件错误:

try
set diskName to "Share"
-- need logic to check if Volume is already mounted
if diskName is in (do shell script "/bin/ls /Volumes") then
    -- Drive is already Mounted,thus can navigate it
    set contentsOfFile to readFile(pathOfFile)
    set isTrue to true   //variable used as a Flag
else
    -- mount Drive 

    mount volume "smb://VolumeToMount/Share"
    set contentsOfFile to readFile(pathOfFile)
    set isTrue to true //variable used as a Flag

end if
end try

这解决了第一个问题,确保服务器/卷安装在本地计算机上,使其可以通过。

第二部分只是实现逻辑来访问该卷路径并使用textFile的值

if isTrue then
  if (contentsOfFile is missing value or (length of contentsOfFile) > 1) then
        tell application "Terminal" Run REST OF CODE
相关问题