如何将文件的文本列表转换为xml格式? (AppleScript的)

时间:2018-01-10 09:34:52

标签: xml file path applescript directory-structure

我有这个将文件列表复制到剪贴板的AppleScript。 该列表包含由换行符分隔的文件夹内容的文件路径。

我一直在寻找Whereisit的替代方案,并将此脚本作为临时解决方案。

目前,文件列表以这种格式显示:

/path/to/file1
/path/to/file2
/path/to/file3

我正在尝试获取xml format中的文件列表,其中包含更多属性。类似于以下格式的东西:

<items>
<item>
<filename>file1.mp3</filename>
<filepath>/path/to/the/file/</filepath>
<tracklength>3:00</tracklength>
<filesize>6mb</filesize>
<artist>artist name</artist>
<title>Song Title</title>
<album>Album Name</album>
</item>
</items>

这是Applescript

property script_name : "List Folder Contents"
property script_description : "This script will list a folder's contents returning full paths as strings and limit the list to specific file types. The script can also process subfolders (recursion)."

set the_folder to (choose folder with prompt "Choose a Folder to List:") as Unicode text
set file_types to {} --file types, set to {} and inc_folders to true to just return folders; file types are 4 character codes such as "osas" or "TEXT"

set with_subfolders to my get_boolean("Search subfolders?", {"No", "Yes"})
if with_subfolders = "user cancelled" then return

set inc_folders to my get_boolean("Include folders in the list? (If you only want file names, and not folder names, select \"No\".)", {"No", "Yes"})
if inc_folders = "user cancelled" then return

set use_posix_path to my get_boolean("Return the paths as HFS (Mac-style) or POSIX (UNIX-style)?", {"HFS", "POSIX"})
if use_posix_path = "user cancelled" then return

set return_as_string to my get_boolean("Return the results as an AppleScript list or a string with each file on its own line?", {"List", "String"})
if return_as_string = "user cancelled" then return

set add_to_clipboard to my get_boolean("Copy the results to the clipboard?", {"No", "Yes"})
if add_to_clipboard = "user cancelled" then return

set the_files to get_folder_list(the_folder, file_types, with_subfolders, inc_folders, use_posix_path)

if return_as_string then
    tell (a reference to my text item delimiters)
        set {old_tid, contents} to {contents, return}
        set {the_files_string, contents} to {the_files as Unicode text, old_tid}
    end tell
    copy the_files_string to the_files
end if

if add_to_clipboard then
    if not return_as_string then
        copy the_files to the_files_string
        repeat with i from 1 to (count the_files_string)
            set item i of the_files_string to ("\"" & item i of the_files_string & "\", ")
        end repeat
        set the_files_string to ("{" & (text 1 thru -3 of (the_files_string as Unicode text)) & "}")
    end if
    set the clipboard to the_files_string
end if
beep
return the_files

on get_folder_list(the_folder, file_types, with_subfolders, inc_folders, use_posix_path)
    set the_files to {}
    tell application "Finder" to set folder_list to items of folder the_folder
    repeat with new_file in folder_list
        try
            set temp_file_type to file type of new_file
        on error
            set temp_file_type to "fold"
        end try
        if file_types contains temp_file_type or file_types = {} then
            if use_posix_path then
                set the_files to the_files & {POSIX path of (new_file as Unicode text)}
            else
                set the_files to the_files & {new_file as Unicode text}
            end if
        end if
        if temp_file_type = "fold" then
            if inc_folders and file_types ≠ {} then
                if use_posix_path then
                    set the_files to the_files & {POSIX path of (new_file as Unicode text)}
                else
                    set the_files to the_files & {new_file as Unicode text}
                end if
            end if
            if with_subfolders then set the_files to the_files & my get_folder_list((new_file as Unicode text), file_types, with_subfolders, inc_folders, use_posix_path)
        end if
    end repeat
    return the_files
end get_folder_list

on get_boolean(m, b)
    try
        return (button returned of (display dialog m buttons ({"Cancel"} & b) default button 3 with icon 1) = (b's item 2))
    on error
        return "user cancelled"
    end try
end get_boolean

我是Applescript的新手,因此不知道如何为xml输出编辑上面的AppleScript。也许在if add_to_clipboard then代码中,可以对以下行进行一些更改:

`set item i of the_files_string to ("\"" & item i of the_files_string & "\", ")`

`set the_files_string to ("{" & (text 1 thru -3 of (the_files_string as Unicode text)) & "}")`

1 个答案:

答案 0 :(得分:0)

尝试以下递归代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication19
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        const string FOLDER = @"c:\temp";
        static XmlWriter writer = null;
        static void Main(string[] args)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            writer = XmlWriter.Create(FILENAME, settings);
            writer.WriteStartDocument(true);

            DirectoryInfo info = new DirectoryInfo(FOLDER);
            WriteTree(info);

            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();


        }
        static long WriteTree(DirectoryInfo info)
        {
            long size = 0;
            writer.WriteStartElement("Folder");
            try
            {
                writer.WriteAttributeString("name", info.Name);
                writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
                writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
                writer.WriteAttributeString("date", info.LastWriteTime.ToString());


                foreach (DirectoryInfo childInfo in info.GetDirectories())
                {
                    size += WriteTree(childInfo);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
            }

            FileInfo[] fileInfo = null;
            try
            {
                fileInfo = info.GetFiles();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
            }

            if (fileInfo != null)
            {
                foreach (FileInfo finfo in fileInfo)
                {
                    try
                    {
                        writer.WriteStartElement("File");
                        writer.WriteAttributeString("name", finfo.Name);
                        writer.WriteAttributeString("size", finfo.Length.ToString());
                        writer.WriteAttributeString("date", info.LastWriteTime.ToString());
                        writer.WriteEndElement();
                        size += finfo.Length;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
                    }
                }
            }

            writer.WriteElementString("size", size.ToString());
            writer.WriteEndElement();
            return size;

        }
    }
}