I am trying to zip the entire files inside a folder using AppleScript. I use this code,
tell application "Finder"
set x1 to (path to home folder as text)
set theItem to x1 & "Documents:MyFolder" as alias
set itemPath to quoted form of POSIX path of theItem
set fileName to name of theItem
set theFolder to POSIX path of (container of theItem as alias)
set zipFile to quoted form of (theFolder & fileName & ".zip")
do shell script "zip -r " & zipFile & " " & itemPath
end tell
This works OK. But it zips the entire folder structure like this inside the zip file,
Users:MyUser:Documents:MyFolder:
I want to just zip the entire files inside the folder without this folder structure. I mean not even the MyFolder. Just the files inside to a zip file.
?
Here's a quick modification that will create zip all of the files inside of itemPath and output them to zipFile:
tell application "Finder"
set x1 to (path to home folder as text)
set theItem to x1 & "Documents:MyFolder" as alias
set itemPath to quoted form of POSIX path of theItem
set fileName to name of theItem
set theFolder to POSIX path of (container of theItem as alias)
set zipFile to quoted form of (theFolder & fileName & ".zip")
do shell script "cd " & itemPath & ";zip -r " & zipFile & " *"
end tell
If you run this as is, it will create MyFolder.zip in Documents containing the contents of MyFolder.
The trick going on here is the script is cd-ing into the folder first, and then recursively call zip on *, which represents all of the files in the current directory (which is the target to be zipped).
Related
The script is fetching properties from a file which resides in a folder, which is most of the time but not always the Frontmost window, e.g. the Desktop. But when the script is checking if the file exists at all this always throws an error, although the file is there. Actually the script is fired only when the file is dropped from a location into a FileMaker container. So it IS there!
It boils down to "set fileExists to (containerName & theFile) as alias exists" gives ":Desktoptext2.txt" which seems that the problem is to build the path.
1) What is wrong in the code below?
2) Is there any other way of fetching some properties of a file where the location is not known yet at the time of evaluation?
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
Although a preference can be set to show the POSIX path in the Finder window, normally the name of a Finder window is just that - the name of the folder it is opened to. The target property of a Finder window is its file reference, so it is better to use that and coerce to the desired path.
When dealing with the Finder, it can get goofy with POSIX stuff, so it is best to avoid that and just coerce when needed. An alias can also be used as a shortcut for the exists test, since the coercion will fail if the item doesn't exist:
property fileName : "text2.txt"
try
tell application "Finder" to set containerName to target of front Finder window as text
set filePath to (containerName & fileName) as alias
on error errMsg -- no file or Finder window
log errMsg
activate
try
set containerName to (choose folder with prompt "Choose A Folder") as text
set filePath to (containerName & fileName) as alias
on error errMsg -- no file or cancel
log errMsg
return
end try
end try
log filePath
tell application "Finder" to set {creation date:creaDate, modification date:modDate, name:fName, displayed name:dName, name extension:nExt, description:descript, URL:fPath} to properties of filePath
set theText to creaDate & "#" & modDate & "#" & fName & "#" & ¬
dName & "#" & nExt & "#" & descript & "#" & fPath
do shell script "echo " & theText & " >> $HOME/Desktop/FileProperties.txt"
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 am attempting to use Xcode and AppleScript-objC to zip a folder containing some network test files but get an error on this section of code.
set homeFolder to (path to home folder)
set homeDesktop to (POSIX file (POSIX path of homeFolder & "/Desktop/NetworkTestResults/"))
tell application "Finder"
set theItem to homeDesktop as alias
set itemPath to quoted form of POSIX path of theItem
set fileName to name of theItem
set theFolder to POSIX path of (container of theItem as alias)
set zipFile to quoted form of (theFolder & fileName & ".zip")
do shell script "zip -jr " & zipFile & " " & itemPath
end tell
The error text is "Can't make <> into type alias. (error -1700)
Any thoughts on how to correct/work around this?
Thanks for the assist.
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 the following applescript that takes files and puts them in their corresponding folders.
set sourceFolder to choose folder
tell application "Finder"
set theFiles to files of sourceFolder
repeat with aFile in theFiles
set fileName to name of aFile
if fileName contains "#" then
set poundOffset to offset of "#" in fileName
set folderName to text 1 thru (poundOffset - 2) of fileName
set newFolder to (sourceFolder as text) & folderName & ":"
if not (exists folder newFolder) then
make new folder at sourceFolder with properties {name:folderName}
end if
move aFile to folder newFolder
end if
end repeat
end tell
It works great for me except in the case of a file conflict. If there's a file with the same name in the folder that it's putting it in the script gets an error and crashes. So my question is this....How do I fix that? I'm willing for it to just overwrite the file but is there a way to bring up a prompt to skip the file or just skip it all together and move on to the next one?
I'm a little fuzzy on what I can do here. Thanks in advance for the help.
Ringslinger.
Try:
move aFile to folder newFolder replacing true