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......
Related
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) ...
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
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