Help I need to print into the output of IDE all of the folder and file names contained in a folder. Here is my code so far:
set Music_Collection_Folder to POSIX file "/Volumes/A_Live HD/Music/Collection" as alias
set the_string to ""
tell application "Finder"
repeat with this_item in (get items of ("/Volumes/A_Live HD/Music/Collection"))
set the_string to the_string & name of this_item & tab & modification date of this_item & tab & size of this_item & tab & kind of this_item & return
end repeat
end tell
You created an alias of the target folder. Use it!
The Finder has no idea what a POSIX path is
repeat with this_item in (get items of Music_Collection_Folder) ...
Related
This works ok, UNLESS there are Spaces (and maybe other special characters) anywhere in the folder path. if there are spaces, then the check for folder exists fails, how can I make it work?
tell application "Finder"
set mySaveASDelimiters to AppleScript's text item delimiters
set myScriptFilePath to (path to me)
set mySourceFolder to folder of myScriptFilePath
set myFileAliasList to the entire contents of mySourceFolder
set myPOSIXSourceFolder to URL of mySourceFolder
set myPOSIXSourceFolder to characters 8 thru -1 of myPOSIXSourceFolder as string
repeat with myFile in myFileAliasList
set myFileName to name of myFile
log "myFileName: " & myFileName
if (myFileName ends with ".txt") then
set AppleScript's text item delimiters to "."
set myFolderName to first text item of myFileName
set AppleScript's text item delimiters to mySaveASDelimiters
set myFolderPath to myPOSIXSourceFolder & myFolderName as string
log "myFolderPath: " & myFolderPath
tell application "System Events"
if not (exists folder myFolderPath) then
beep
display dialog "Could not find Folder for File: " & myFileName
return {}
end if
end tell
end if
end repeat
end tell
Lets make this easier:
set myScriptFilePath to (path to me)
set mySourceFolder to folder of myScriptFilePath
set myPOSIXSourceFolder to POSIX path of mySourceFolder
I get an error on myPOSIXSourceFolder, why! I just want the current folder path in POSIX notation! I have fixed it now:
set myAppFile to (path to me)
set myAppFilePath to ((path to me as text) & "::") --Magic I was looking for
set myAppFolder to POSIX path of (myAppFilePath) as string
set myFilePath to myAppFolder & "ModTest1"
log "myAppFilePath: " & myAppFilePath
log "myAppFile: " & myAppFile
log "myAppFolder: " & myAppFolder
log "myFilePath: " & myFilePath
set thePath to myFilePath
tell application "System Events"
if exists folder thePath then
beep
end if
end tell
set myAppFile to (path to me)
set myAppFilePath to POSIX path of ((path to me as text) & "::") --Magic I was looking for
This is the answer I was looking for, its basically the syntax takes a bit of getting used to, one of the problems with AppleScript......
This code runs, but for some reason no files are saved after running it and no errors appear. Ideas?
AppleScript
tell application "Finder"
set fl to files of folder POSIX file "/Users/abc/folder" as alias list
end tell
repeat with f in fl
tell application "Microsoft Word"
activate
open f
set theActiveDoc to the active document
end tell
delay 1
tell application "System Events"
keystroke "a" using command down
keystroke "c" using command down
end tell
tell application "Finder"
set filename to name of f
set the_path to POSIX file "/Users/abc/folder2/" & filename
end tell
tell application "Microsoft Word"
close theActiveDoc saving no
set new_document to make new document
paste special (text object of selection) data type paste text
save as new_document file name the_path
close active document
end tell
end repeat
solution
set the_path to (POSIX file "/Users/abc/folder2/" as text) & filename
Here is a completely different route. This solution bypasses having to use Microsoft Word, completely. I've configured this script to loop through every file in a designated folder (assuming all of the files in this folder are either Microsoft word documents, plain text files, or rich text files)… takes the content of each file, and creates a new .docx file with that content in a new folder
If this code is saved as an application, you can drag files and or folders, directly onto this application's Icon, to be processed. If this application is directly launched, it will ask you whether you want to process a file or files, or process a folder with all of its contents
on open theFiles2
-- Handle the case where the script is launched by dropping
-- file or folders onto this applet's icon
tell application "Finder"
set theFiles to files of entire contents of folder theFiles2 as alias list
end tell
repeat with aFile from 1 to count of theFiles
set thisFile to item aFile of theFiles
set thisFile2 to thisFile
set thisFile to POSIX path of thisFile
tell application "Finder"
set fileName to name of thisFile2
set fileName to (characters -5 thru 1) of fileName as string
set thePath to (path to desktop as text) & fileName
try
set newFolder to make new folder ¬
at (path to desktop) ¬
with properties {name:fileName}
end try
end tell
set thePath to POSIX path of alias thePath
set theScript to do shell script "textutil -convert docx " & quoted form of ¬
thisFile & " -output " & quoted form of thePath & quoted form of fileName & ".docx"
end repeat
end open
on run
-- Handle the case where the script is launched without any dropped files
set sourceFolder to display dialog ¬
"Would You Like To Choose A Folder With All Of Its Contents Or Individual Files Within A Folder?" buttons {"Cancel", "Select Files", "Select Folder"} ¬
default button 3 ¬
cancel button ¬
"Cancel" with title ¬
"withTitleText" with icon 1 ¬
giving up after 20
if button returned of sourceFolder is "Select Folder" then
set sourceFolder to ((choose folder) as text)
tell application "Finder"
set theFiles to files of entire contents of folder sourceFolder as alias list
end tell
repeat with aFile from 1 to count of theFiles
set thisFile to item aFile of theFiles
set thisFile2 to thisFile
set thisFile to POSIX path of thisFile
tell application "Finder"
set fileName to name of thisFile2
try
set fileName to (characters -5 thru 1) of fileName as string
end try
set thePath to (path to desktop as text) & fileName
try
set newFolder to make new folder ¬
at (path to desktop) ¬
with properties {name:fileName}
end try
end tell
set thePath to POSIX path of alias thePath
set theScript to do shell script "textutil -convert docx " & quoted form of ¬
thisFile & " -output " & quoted form of thePath & quoted form of fileName & ".docx"
end repeat
else if button returned of sourceFolder is "Select Files" then
set theFiles to choose file with prompt ¬
"Choose Files" invisibles false ¬
multiple selections allowed true ¬
as text
repeat with aFile from 1 to count of theFiles
set thisFile to item aFile of theFiles
set thisFile2 to thisFile
set thisFile to POSIX path of thisFile
tell application "Finder"
set fileName to name of thisFile2
try
set fileName to (characters -5 thru 1) of fileName as string
end try
set thePath to (path to desktop as text) & fileName
try
set newFolder to make new folder ¬
at (path to desktop) ¬
with properties {name:fileName}
end try
end tell
set thePath to POSIX path of alias thePath
set theScript to do shell script "textutil -convert docx " & quoted form of ¬
thisFile & " -output " & quoted form of thePath & quoted form of fileName & ".docx"
end repeat
end if
end run
This code may be a little sloppy and there may b a better solution but... It seems to work pretty well on my system running the latest version of macOS High Sierra
I have a folder that is three levels deep, each level contains a good amount of files. I am able to get two levels deep with the script below, however the third level is posing a bit of a challenge. Would someone mind offering some guidance as to how I am supposed to get one level deeper? Use python or another language wouldn't be acceptable, as I am trying to see how this works with AppleScript.
set sourceFolder to (choose folder)
tell application "Finder"
my changeFileNameCase(sourceFolder, "upper")
repeat with subFolder in (get every folder of folder sourceFolder)
my changeFileNameCase(subFolder as alias, "upper")
#This Is No Good
repeat with theFolder in (get every folder of folder subFolder)
my changeFileNameCase(theFolder, "upper")
end repeat
end repeat
end tell
on changeFileNameCase(targetFolder, caseToSwitchTo)
tell application "Finder"
set fileList to every file of folder targetFolder
repeat with theFile in fileList
set oldName to name of theFile
set newName to my changeCaseOfText(oldName, caseToSwitchTo)
set the name of theFile to newName
end repeat
end tell
end changeFileNameCase
To make the handler recursive you have to get all items of the folder and check the class of each item.
If the class is folder call the handler passing the folder
If the class is file rename it
set sourceFolder to (choose folder)
changeFileNameCase(sourceFolder, "upper")
on changeFileNameCase(targetFolder, caseToSwitchTo)
tell application "Finder"
set theList to every item of targetFolder
repeat with i from 1 to count theList
set theItem to item i of theList
if class of theItem is folder then
my changeFileNameCase(theItem, caseToSwitchTo)
else
set oldName to name of theItem
set newName to my changeCaseOfText(oldName, caseToSwitchTo)
set name of theItem to newName
end if
end repeat
end tell
end changeFileNameCase
Im struggling to understand how to rename(append) filenames inside a folder with the folders name.
Eg, about.txt, picture.jpg, etc to become folderName-about.txt, folderName-picture, etc.
Im a complete noob at applescript, this is my first script ever!
on run {input, parameters}
tell application "Finder"
set theFolder to folder "OS X:Users:David:Desktop:SCRIPT:script-copy"
set targetFolder to folder "OS X:Users:David:Desktop:SCRIPT:script-copy-finished"
display dialog "Which structure do you wish to duplicate?" buttons {"Structure-MAIN", "Structure-OTHER"}
set chosenStructure to button returned of result
if contents of chosenStructure is equal to "Structure-MAIN" then
set chosenStructure to "OS X:Users:David:Desktop:SCRIPT:script-copy:-MAIN"
else
set chosenStructure to "OS X:Users:David:Desktop:SCRIPT:script-copy:-OTHER"
end if
display dialog "Specify a new folder name:" default answer "John The Dog"
set newName to (text returned of result)
set createNewStructure to make new folder at targetFolder with properties {name:newName}
duplicate every file of entire contents of folder chosenStructure to createNewStructure
set the_folder to name of folder chosenStructure
repeat with this_file in (get files of entire contents of folder chosenStructure)
set the_start to offset of "_" in ((name of this_file) as string)
set the_stop to count (name of this_file as string)
set name of this_file to (the_folder & (items the_start thru the_stop of (name of this_file as string)))
end repeat
end tell
return input
end run
Thanks for your help!
I want all filenames inside the folder to have the folders name
prepended at the beginning of the filename. Eg, about.txt to become
folderName-about.txt, etc.
Try this:
tell application "Finder"
...
set the_folder to name of createNewStructure
repeat with this_file in (get files of entire contents of createNewStructure)
set name of this_file to the_folder & "-" & (name of this_file)
end repeat
end tell
The whole Script:
set theFolder to ((path to desktop) as text) & "SCRIPT:script-copy"
set targetFolder to ((path to desktop) as text) & "SCRIPT:script-copy-finished"
display dialog "Which structure do you wish to duplicate?" buttons {"-MAIN", "-OTHER"}
set chosenStructure to ((path to desktop) as text) & "SCRIPT:script-copy:" & button returned of result
display dialog "Specify a new folder name:" default answer "John The Dog"
set newName to (text returned of result)
tell application "Finder"
set createNewStructure to make new folder at targetFolder with properties {name:newName}
duplicate every file of entire contents of folder chosenStructure to createNewStructure
set the_folder to name of createNewStructure
repeat with this_file in (get files of entire contents of createNewStructure)
set name of this_file to the_folder & "-" & (name of this_file)
end repeat
end tell
I have a text file of around 500 selected file names (each on its own line) from an event I photographed with over 3,000 pictures. I want to be able to find all those images in the folder, duplicate them, and move those duplicated files into a different folder.
This is what I have so far. Right now it just copies the entire folder of 3,000 images and puts it into the destination folder, but not the individual files..
TEXT FILE:
_EPW0847.jpg
_EPW0848.jpg
_EPW0853.jpg
etc....
APPLESCRIPT:
set thePhotos to paragraphs of (read (choose file with prompt "Choose a text file"))
set theSourceFolder to (choose folder with prompt "Choose source folder") as string
set theDestination to (choose folder with prompt "Choose destination folder")
repeat with theName in thePhotos
try
tell application "Finder" to duplicate alias (theSourceFolder & theName) to theDestination with replacing
end try
end repeat
tell application "Finder"
tell folder theDestination to set theCount1 to (count of items) as string
end tell
set theCount2 to (count of thePhotos) as string
display dialog (theCount1 & " of " & theCount2 & " items copied to " & theDestination) buttons {"OK"}
Any help would be great. I don't know apple script that well yet, but I'm learning. Thanks!
You were close!
set thePhotos to paragraphs of (read (choose file with prompt "Choose a text file"))
set theSourceFolder to (choose folder with prompt "Choose source folder")
set theDestination to (choose folder with prompt "Choose destination folder")
set dupeList to {}
repeat with theName in thePhotos
try
set end of dupeList to alias ((theSourceFolder as text) & theName)
end try
end repeat
tell application "Finder" to duplicate dupeList to theDestination with replacing
set theCount1 to (count of dupeList) as text
set theCount2 to (count of thePhotos) as text
display dialog (theCount1 & " of " & theCount2 & " items copied to " & (theDestination as text)) buttons {"OK"}
If there are blank lines in the text file, try this:
set thePhotos to paragraphs of (read (choose file with prompt "Choose a text file"))
set theSourceFolder to (choose folder with prompt "Choose source folder")
set theDestination to (choose folder with prompt "Choose destination folder")
set dupeList to {}
repeat with theName in thePhotos
try
if theName ≠ "" then
set end of dupeList to alias ((theSourceFolder as text) & theName)
end if
end try
end repeat
set fileContents to read (choose file with prompt "Choose a comma-delimited text file")
set theText to result
set AppleScript's text item delimiters to ","
set theTextItems to text items of theText
set AppleScript's text item delimiters to {""}
theTextItems
set theSourceFolder to (choose folder with prompt "Choose source folder") as string
set theDestination to (choose folder with prompt "Choose destination folder")
repeat with theEPSName in theTextItems
tell application "Finder"
set theEPSFile to theSourceFolder & theEPSName
move file theEPSFile to folder theDestination with replacing
end tell
end repeat