如何使用 AppleScript 获取文件的属性或信息?

时间:2021-03-21 22:37:11

标签: applescript

我需要将我只有名称(此处:“text2.txt”)的文件的属性或至少信息写入文本文件。 我尝试了各种方法,但每次我只得到桌面文件夹的属性而不是文件。

我在这里做错了什么?

try
    tell application "Finder"
        set this_folder to path of (folder of the front Finder window) as alias
    end tell
on error
    -- no open folder windows
    set this_folder to path to desktop folder as alias
    set is_desktop to true
end try

tell application "System Events"
    set file_list to the name of every file of this_folder
end tell

tell application "Finder"
    set theFile to path to desktop folder
    set myList to {}
    repeat with n from 1 to count of file_list
        set currentFile to item n of file_list
        set the_filePath to this_folder
        set the_filename to currentFile
        if currentFile contains "text2.txt" then
            set {creation date:creaDate, modification date:modDate, name:fName, displayed name:dName, name extension:nExt, description:descript, URL:fPath} to properties of the_filePath
            set theText to creaDate & "#" & modDate & "#" & fName & "#" & dName & "#" & nExt & "#" & descript & "#" & fPath
            do shell script "echo " & theText & ">> $HOME/Desktop/FileProperties.txt"
        end if
    end repeat
end tell

2 个答案:

答案 0 :(得分:0)

我对您的代码进行了一些调整。也许这样的事情会帮助你。

property theFile : "text2.txt"

tell application "Finder"
    try
        -- Gets The Name Of The Front Finder Window If There Is One Opened
        set containerName to name of front Finder window as POSIX file as text
        -- Checks If The File "text2.txt" Exists In That Folder
        -- fileExists Will Be Set To True Or False Depending On The Results
        set fileExists to (containerName & theFile) as alias exists
    on error errMsg number errNum
        -- If Either Of The Previous Commands Throws An Error
        -- This Will Give You An Option To Choose A Folder Where You Think
        -- The File "text2.txt" Is Located
        activate
        set containerName to my (choose folder with prompt "Choose A Folder") as text
    end try
    try
        -- Checks If The File "text2.txt" Exists In The New Chosen Folder
        set fileExists to (containerName & theFile) as alias exists
    on error errMsg number errNum
        -- If "text2.txt" Does Not Exist In This Folder Either, Script Stops Here
        return
    end try
    delay 0.1
    -- If fileExists Is Set To True From Previous Commands 
    if fileExists then
        set fullFilePath to (containerName & theFile) as alias
        set {creation date:creaDate, modification date:modDate, name:fName, displayed name:dName, name extension:nExt, description:descript, URL:fPath} to properties of fullFilePath
        set theText to creaDate & "#" & modDate & "#" & fName & "#" & ¬
            dName & "#" & nExt & "#" & descript & "#" & fPath
        tell current application to (do shell script "echo " & theText & ¬
            ">> $HOME/Desktop/FileProperties.txt")
    end if
end tell

答案 1 :(得分:0)

我认为你已经基本掌握了。

Finder 窗口有一个“目标”,即窗口查看的文件夹。您可以正常获取和设置目标。 try 语句下面的所有内容都保持不变。

try
    tell application "Finder" to set this_folder to target of front window as alias
    
on error
    set this_folder to (path to desktop) as alias
    set is_desktop to true
end try

作为替代方案,脚本可以使用 TextEdit 或它自己的“写入”命令来创建文本文档。如果您好奇,请告诉我,我会添加这些选项。


好的……这是一个您可以尝试的脚本,它可以处理 Applescript 中的书写。我做了一些其他的调整,所有这些都被注释了(不幸的是,这使得脚本看起来比实际更长)。

Applescript 有一个内置的“文件读/写套件”,您可以在语言指南 here 中阅读每个命令,但简而言之,您使用这些命令写入新文件,替换或将文本附加到现有文件。

附言我改变了你的一些变量名

-- Make front window specifier a global var. Unnecessary if you integrate System Events block into Finder block (see below)
global fw

tell application "Finder"
    try
        set targFol to target of front window as alias
        set fw to front window
    on error
        set targFol to (path to desktop) as alias
        set fw to make Finder window to targFol
    end try
    
end tell

Finder 也可以处理下面的系统事件块。如果您正在通过的文件夹中通常没有数千个文件(或者如果替代行就足够了),您应该考虑这样做,因为使用系统事件的主要优势是具有高文件数的性能。我认为除了删除适当的“告诉/结束”语句之外不需要任何更改。

tell application "System Events"
    -- Some filtering options as an alternative to the repeat loop
    set txFiles1 to files of targFol whose kind is "Plain Text Document"
    set txFiles2 to files of targFol whose kind is "Plain Text Document" and name is "text2.txt"
    set txFile to item 1 of txFiles2
    
    -- Alternatively, you can just use the file itself, without any of the rigmarole.
    set txFile to file "text2.txt" of targFol
end tell

tell application "Finder"
    set {creation date:creaDate, modification date:modDate, name:fName, displayed name:dName, name extension:nExt, description:descript, URL:fPath} to properties of targFol
    
    set newText to creaDate & "#" & modDate & "#" & fName & "#" & dName & "#" & nExt & "#" & descript & "#" & fPath as text
end tell

-- Write the properties as text to a text file -- requires the 'class furl' bit to work
set tf to (path to desktop as text) & "FileProperties.txt" as «class furl»
-- Alternatively, you could create the text file in the target folder 
-- set tf to (targFol as text) & "FileProperties.txt" as «class furl»

close access (open for access tf)
write newText to tf as text

-- And some obvious things you can do with your new text file
tell application "TextEdit" to open tf
tell application "Finder" to select tf
set rtext to read tf
相关问题