问题描述:

I actually have two questions.

How to exclude hidden files like .DS_STORE, Icon when I try to get files in folder ?

I've tried "without invisibles" but it seems not working.

How to set my var the_new_folder as an existing folder if already exists ?

Thanks for answers.

My code:

--

-- Get all files in a selected folder

-- For each file, create a folder with the same name and put the file in

--

tell application "Finder"

set the_path to choose folder with prompt "Choose your folder..."

my file_to_folder(the_path)

end tell

on file_to_folder(the_folder)

tell application "Finder"

-- HELP NEEDED HERE

-- HOW TO EXCLUDE HIDDEN FILES (Like Icon, .DS_STORE, etc)

set the_files to files of the_folder

repeat with the_file in the_files

-- Exclude folder in selection

if kind of the_file is not "Folder" then

set the_path to container of the_file

set the_file_ext to name extension of the_file

-- Remove extension of the file name

set the_file_name to name of the_file as string

set the_file_name to text 1 thru ((offset of the_file_ext in (the_file_name)) - 2) of the_file_name

-- Make the new folder with the file name

try

set the_new_folder to make new folder at the_path with properties {name:the_file_name}

on error

-- HELP NEEDED HERE

-- HOW TO SET the_new_folder AS THE EXISTING FOLDER

end try

-- Move the file in the new folder

move the_file to the_new_folder

end if

end repeat

end tell

end file_to_folder

tell application "Finder"

(display dialog ("It's done!") buttons {"Perfect!"})

end tell

网友答案:

Using the System Events context instead of Finder:

  • bypasses the problem with the AppleShowAllFiles preference[1]

  • is much faster in general.

Using the visible property of file / folder objects in the System Events context allows you to predictably determine either all items, including hidden ones (by default), or only the visible ones (with whose visible is true):

# Sample input path.
set the_path to POSIX path of (path to home folder)

tell application "System Events"

    set allVisibleFiles to files of folder the_path whose visible is true

end tell

Simply omit whose visible is true to include hidden files too.


The code for either referencing a preexisting folder or creating it on demand is essentially the same as in the Finder context:

# Sample input path.
set the_path to POSIX path of (path to home folder)

# Sample subfolder name
set the_subfolder_name to "subfolder"

tell application "System Events"

    if folder (the_path & the_subfolder_name) exists then
        set subfolder to folder (the_path & the_subfolder_name)
    else
        set subfolder to make new folder at folder the_path ¬
          with properties {name: the_subfolder_name}
    end if

end tell

[1] In order to predictably exclude hidden items, a Finder-based solution is not only cumbersome but has massive side effects:

  • You need to determine the current state of the the AppleShowAllFiles preference (defaults read com.apple.Finder AppleShowAllFiles),
  • then turn it off.
    • then kill Finder to make the change take effect (it restarts automatically) - this will be visually disruptive
  • then, after your code has run, restore it to its previous value.
    • then kill Finder again so that the restored value takes effect again.
网友答案:

I believe that your first question is not a problem. Your code works fine for me.

As for the second issue, the simplest method is to use the text representation of the_path, and simply build the new folder, and see if it already exists:

set the_path_Text to (the_path as text)
set try_Path to the_path_Text & the_file_name
if (folder try_Path) exists then
    set the_new_folder to (folder try_Path)
else
    set the_new_folder to make new folder at the_path with properties {name:the_file_name}
end if

If you are truly having difficulty with the first code section, please post a new question with more details, such as a copy of the Result section of the Script.

网友答案:

Thank you to all of you ! @mklement0 @craig-smith

You will find below the corrected code with your help!

If I share this code, you want to be cited for thanks?

--
-- Get all files in a selected folder
-- For each file, create a folder with the same name and put the file in
--


-- Get user folder
set the_path to choose folder with prompt "Choose your folder..."
my file_to_folder(the_path)


on file_to_folder(the_folder)

    tell application "System Events"

        -- Get all files without hidden
        set the_files to files of the_folder whose visible is true

        repeat with the_file in the_files

            -- Remove extension of the file name
            set the_file_ext to name extension of the_file
            set the_file_name to name of the_file as string
            set the_file_name to text 1 thru ((offset of the_file_ext in (the_file_name)) - 2) of the_file_name

            -- Make a new folder or get the existing
            set the_path to POSIX path of the_folder
            if folder (the_path & the_file_name) exists then
                set the_new_folder to folder (the_path & the_file_name)
            else
                set the_new_folder to make new folder at folder the_path with properties {name:the_file_name}
            end if

            -- Move the file to the new folder
            move the_file to the_new_folder

        end repeat

    end tell
end file_to_folder


-- Ending dialog
display dialog ("It's done!") buttons {"Perfect!"}
相关阅读:
Top