set chosenFolder to (choose folder)
try
tell application "System Events" to set fileNameList to name of files of chosenFolder whose visible is true
on error errMsg
display dialog "ERROR: " & errMsg
error number -128
end try
set sourceFolder to chosenFolder as text
set {TID, text item delimiters} to {text item delimiters, "_"}
repeat with fileName in fileNameList
set prefix to (text items 1 thru -3 of fileName) as text
set sourceFile to quoted form of POSIX path of (sourceFolder & fileName)
set destinationFolder to POSIX path of (sourceFolder & prefix)
set destinationFile to quoted form of (destinationFolder & "/" & fileName)
do shell script "/bin/mkdir -p " & quoted form of destinationFolder & " ; /bin/mv " & sourceFile & space & destinationFile
end repeat
set text item delimiters to TID
Thank you very much in advance for your very kind help.
Model: mac mini 2011
AppleScript: 2.6.1
Browser: Safari 537.36
Operating System: Mac OS X (10.9)
I tried to modify the code of the original thread to the below script but nothing happens.
When I run the script I choose the folder B (where are my files) then the code stops, folder A (subfolders) and folder B (files) remains the same.
I cannot figure it out which part of the code I have to change to make it work
Deeply appreciate any help.
set chosenFolder to (choose folder)
try
tell application "System Events" to set fileNameList to name of files of chosenFolder whose visible is true
on error errMsg
display dialog "ERROR: " & errMsg
error number -128
end try
set sourceFolder to chosenFolder as text
repeat with fileName in fileNameList
set sourceFile to quoted form of POSIX path of (sourceFolder & fileName)
set destinationFolder to POSIX path of (sourceFolder)
set destinationFile to quoted form of (destinationFolder & "/" & fileName)
end repeat
Yes, I didn’t notice that moment you edited the code in the same post.
I thought you’d edit the code in a new post. I apologize.
In this new request, what I am trying to accomplish is to move my files, each to its own folder according to its name.
But, here is the thing. Later, I have to rename manually each folders name.
Then, with your other code I will rename all the files of the new renamed folders.
I do not know if it is the best way of doing what I want. But it is what I figured it out.
Any help would be appreciated. If cannot, I thank you a lot anyway.
# for http://macscripter.net/viewtopic.php?id=45537
# You may change the folder names without renaming the property names
property folderA : "Folder A"
property folderB : "Folder B"
-- set theFolder to (path to desktop as text) & "Docs" # defines a text object
set theFolder to (choose folder with prompt "Select a folder:") # defines an alias object
tell application "System Events"
if class of theFolder is text then set theFolder to theFolder as alias
if (not (exists folder folderA of theFolder)) or (not (exists folder folderB of theFolder)) then error "The folder "" & theFolder & "" doesn't match the script requirements !"
set rootSource to path of (folder folderB of theFolder) as text
set rootTarget to path of (folder folderA of theFolder) as text
set theFileNames to name of files of folder folderB of theFolder whose visible is true
end tell
set oldBeg to ""
repeat with aFileName in theFileNames
set itsBeg to item 1 of my decoupe(aFileName as text, ".")
if itsBeg is not oldBeg then
if oldBeg ≠"" then
do shell script "/bin/mkdir -p " & itsNewLocation & "; /bin/mv " & my recolle(listSourceFiles, space) & space & itsNewLocation
end if
set listSourceFiles to {}
set oldBeg to itsBeg
end if
set sourcefile to quoted form of POSIX path of (rootSource & aFileName)
set end of listSourceFiles to sourcefile
set itsNewLocation to quoted form of POSIX path of (rootTarget & itsBeg)
end repeat
do shell script "/bin/mkdir -p " & itsNewLocation & "; /bin/mv " & my recolle(listSourceFiles, space) & space & itsNewLocation
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
on recolle(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
#=====
Running the code when I select a folder I choose Folder B (where are the files) then I get the next error:
Folder B:" doesn’t match the script requirements !" number -2700 from «script» to item
I have the 2 folders:
Folder A with the subfolders
Folder B with the files
each subfolder and each file have the same name
I do not want to be a pain in the neck, but I imagine the code something like ask me:
choose the folder where are your files
choose the folder where are your subfolders
then move each file to their correspondent subfolder
I apologize but I gave an answer to the original question.
As the two folders were in a single one, it was logical to select only one folder, the one containing the two others.
Now you change it.
You will have to wait a bit for an answer.
I hope that this time it’s what you wanted.
Now the two folders aren’t forced to be in the same one.
# for http://macscripter.net/viewtopic.php?id=45537
--set sourceFolder to (path to desktop as text) & "Docs:Folder B" # defines a text object
--set storageFolder to (path to desktop as text) & "Docs:Folder A" # defines a text object
set sourceFolder to (choose folder with prompt "Select a folder containing your files:") # defines an alias object
set storageFolder to (choose folder with prompt "Select a folder supposed to receive the files:") # defines an alias object
tell application "System Events"
if class of storageFolder is alias then set storageFolder to storageFolder as text
if class of sourceFolder is alias then set sourceFolder to sourceFolder as text
if storageFolder does not end with ":" then set storageFolder to storageFolder & ":"
if sourceFolder does not end with ":" then set sourceFolder to sourceFolder & ":"
set theFileNames to name of files of folder sourceFolder whose visible is true
end tell
set oldBeg to ""
repeat with aFileName in theFileNames
set itsBeg to item 1 of my decoupe(aFileName as text, ".")
if itsBeg is not oldBeg then
if oldBeg ≠"" then
do shell script "/bin/mkdir -p " & itsNewLocation & "; /bin/mv " & my recolle(listSourceFiles, space) & space & itsNewLocation
end if
set listSourceFiles to {}
set oldBeg to itsBeg
end if
set sourcefile to quoted form of POSIX path of (sourceFolder & aFileName)
set end of listSourceFiles to sourcefile
set itsNewLocation to quoted form of POSIX path of (storageFolder & itsBeg)
end repeat
do shell script "/bin/mkdir -p " & itsNewLocation & "; /bin/mv " & my recolle(listSourceFiles, space) & space & itsNewLocation
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
on recolle(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
#=====
A detail is puzzling me.
In your other thread you have files named like :
prefix + a string or nothing + dot + extension
In this thread, your question didn’t list such filenames but are we sure that you don’t have such files in the source file?
The script doesn’t treat such case. For it the filenames are : prefix + dot + extension.
# for http://macscripter.net/viewtopic.php?id=45537
--set sourceFolder to (path to desktop as text) & "Docs:Folder B" # defines a text object
--set storageFolder to (path to desktop as text) & "Docs:Folder A" # defines a text object
set sourceFolder to (choose folder with prompt "Select a folder containing your files:") # defines an alias object
set storageFolder to (choose folder with prompt "Select a folder supposed to receive the files:") # defines an alias object
tell application "System Events"
if class of storageFolder is alias then set storageFolder to storageFolder as text
if class of sourceFolder is alias then set sourceFolder to sourceFolder as text
if storageFolder does not end with ":" then set storageFolder to storageFolder & ":"
if sourceFolder does not end with ":" then set sourceFolder to sourceFolder & ":"
set theFileNames to name of files of folder sourceFolder whose visible is true
end tell
set oldBeg to ""
repeat with aFileName in theFileNames
set itsBeg to item 1 of my decoupe(aFileName as text, ".")
if itsBeg is not oldBeg then
if oldBeg ≠"" then
tell application "System Events"
if exists folder itsnewlocation then
my moveOrigInTarget(listSourceFiles, itsnewlocation)
end if
end tell
end if
set listSourceFiles to {}
set oldBeg to itsBeg
end if
set sourcefile to quoted form of POSIX path of (sourceFolder & aFileName)
set end of listSourceFiles to sourcefile
set itsnewlocation to POSIX path of (storageFolder & itsBeg)
end repeat
tell application "System Events"
if exists folder itsnewlocation then
my moveOrigInTarget(listSourceFiles, itsnewlocation)
end if
end tell
#=====
on moveOrigInTarget(listSourceFiles, itsnewlocation)
do shell script "mv " & my recolle(listSourceFiles, space) & space & quoted form of itsnewlocation
end moveOrigInTarget
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
on recolle(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
#=====
Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) mardi 28 mars 2017 09:56:55