I'm trying to create a script that copies a template folder to current finder window then renames the new folder and a couple of files within it based on dialogue input.
So far, I've got it so it can copy a folder (from a selection) to the current finder window and rename it. But I can't get it to then rename a file within it.
Here's the code -
property A : POSIX file "/BLOCKED OUT FOR PRIVACY/" as alias
property B : POSIX file "/BLOCKED OUT FOR PRIVACY/" as alias
property C : POSIX file "/BLOCKED OUT FOR PRIVACY/" as alias
property D : POSIX file "/BLOCKED OUT FOR PRIVACY/" as alias
tell application "Finder"
set x to target of window 1 as alias
end tell
set JobName to text returned of (display dialog "Enter Folder Name:" default answer "Template Folder")
set CATno to text returned of (display dialog "Enter CAT number:" default answer "CMXX0000")
set optionList to {"OPTION 1", "OPTION 2", "OPTION 3", "OPTION 4"}
set chosenFolder to choose from list optionList with prompt "Choose a Folder"
set chosenFolder to chosenFolder's item 1
if chosenFolder is "OPTION 1" then
tell application "Finder"
set FolderCopy to duplicate B to x
set the name of FolderCopy to JobName
set Insert to (POSIX path of (path to home folder)) & "DVD Insert Artwork/Indesign Project File/_Insert.indd" as POSIX file
set the name of Insert to JobName & CATno
end tell
end if
I've cut out the POSIX file paths as they contain my company name and such. I've also left out the other parts of the if as they'll essentially be duplicates of the first.
set Insert to (POSIX path of (path to home folder)) & "DVD Insert Artwork/Indesign Project File/_Insert.indd" as POSIX file
set the name of Insert to JobName & CATno
This is the part that's giving me trouble. It should be renaming a file that is within the newly duplicated folder to the what's entered in the CATno dialog box + "_Insert.indd"
Any help is greatly appreciated!
Thanks :)
I'm not sure but do you mean this?
tell application "Finder"
set FolderCopy to duplicate B to x
set insertFile to file "_Insert.indd" of FolderCopy
set the name of FolderCopy to JobName
set name of insertFile to (JobName & CATno & "_Insert.indd")
end tell
The gets the reference to the indesign file from the duplicated folder.
Related
I'm creating an Automator workflow to cycle through all the tabs of a Google Chrome window and save the webpage on each tab to a text file.
on run {input, parameters}
tell application "Google Chrome"
set windowList to every window
repeat with theWindow in windowList
set tabList to every tab in theWindow
repeat with theTab in tabList
select all
copy selection
set theTitle to title of theTab
set theScript to "echo" & (selection as text) & "> $HOME/Desktop/tmp" & quoted form of POSIX path of theTitle & "-clipboard-file.txt"
display dialog theScript
do shell script theScript
end repeat
end repeat
end tell
return input
end run
However this generates empty text files.
I suspect that the "copy selection" does not interact with the system pasteboard. Is there a way I can copy the text into paste board or export directly to text files?
[This has been [edited] to include the magic unicode-friendly "coercion". See the updated line under the "unicode-friendly" comment. The line uses the famous AppleEvent code option-backslash and option-shift-backslash characters]
It seems you just want the text, as if it were copied to the clipboard, saved to each file.
I would caution you about using the method you've started out with, only because there may be situations where no title leaves you with a file actually named ".txt", which would be invisible! So I'd say use a counting variable along with what you have, just in case (this worked on my machine and has a safeguard for untitled pages):
--first, I get the desktop as an old mac style path, using the Finder:
tell application "Finder" to set dt to desktop as string
tell application "Google Chrome"
set windowList to every window
set tabCount to 1
repeat with theWindow in windowList
set tabList to every tab in theWindow
repeat with theTab in tabList
set wHTMLText to execute theTab javascript "document.body.innerText;"
set thetitle to title of theTab
if thetitle is "" then set thetitle to ("untitled" & (tabCount as string))
set filePath to (dt & "tmp:" & thetitle & ".txt")
set myFile to open for access filePath with write permission
--unicode-friendly:
write wHTMLText to myFile as «class utf8»
close access myFile
set tabCount to tabCount + 1
end repeat
end repeat
end tell
[EDIT:] Oh, and if you want the HTML, use "document.body.innerHTML;" and save with ".html" extension.
I took a little bit of a different approach. This script works but I still can't figure out how to paste the items as a list
tell application "Google Chrome"
every window
URL of every tab of item 1 of result
set the clipboard to the result as text
end tell
do shell script "pbpaste > ~/Desktop/ClipboardFile.txt"
I have a list of image filenames (csv file) and I want to check if these images exist in a folder of images. If the image exists I want to copy it over to new folder.
I built a sample code that runs through without error and duplicated the first matching file to the destination folder correctly. However the rest of the files it returns that it doesn't find a match although they are clearly present in the source folder. Any ideas what is going wrong?
--Step 1. Get file path and read file.
set csvFile to (choose file with prompt "Select CSV file...") as text
set csvRecords to read file csvFile using delimiter {return}
set sourceFolder to (path to documents folder as text) & "AllProducts:"
set destinationFolder to (path to documents folder as text) & "SelectedProducts:"
--Step 2. Process each record (data row) by first extracting the record data as text items.
repeat with thisRecord in csvRecords
set recordItems to my getItems(thisRecord)
-- Enter processing code here
set filePath to sourceFolder & thisRecord
log filePath
tell application "Finder"
if (exists file filePath) then
duplicate file filePath to folder destinationFolder
else
log "File doesn't exist" & thisRecord
end if
end tell
end repeat
--Subroutine that returns the text items from each record.
on getItems(theRecord)
copy the text item delimiters to origDelims
set the text item delimiters to ","
set recordItems to {}
set recordItems to every text item of theRecord
set the text item delimiters to origDelims
return recordItems
end getItems
My incomplete file path is stored in Me!txtFilePath. It contains a complete path to the folder's location and the first 9 out of ~30 characters of the folder name. Those first 9 characters are guaranteed to be unique. Currently I have a button that executes the code below when clicked:
Shell "explorer """ & Me!txtFilePath & "", vbNormalFocus
How would I make sure it opens the right folder given that I don't have the last several characters of the folder name? Thanks!
edit: I am trying to open a network folder that I don't have a "drive" for.
I'm not sure why exactly this solution is the one that works for me whereas the suggestions did not, but I found the answer. Most of it was solved by the commenters to my original post - thanks to all of you.
Basically, I had to set a string variable equal to the Dir value. Dir only returns the ending portion of the folder name, not the entire path, so when opening the file I needed to combine it with the known beginning of the path name. I also had to use Application.FollowHyperlink instead of Shell. I don't know why and I don't know if there are any drawbacks. Maybe Shell explorer only works for files, not folders.
Private Sub btnJobFile_Click()
Dim strStartFilePath As String
Dim strEndFilePath As String
strStartFilePath = "\\JobFolders" & Me!txtFilePath
strEndFilePath = Dir(strStartFilePath & Me.JobNum & "*", vbDirectory)
Application.FollowHyperlink strStartFilePath & strEndFilePath
End Sub
My knowledge of coding is limited. I am trying to automate a process of converting PPTs to HTML using applescript and Keynote. In this page I found the following non working apple script:
-- THE DESTINATION FOLDER
-- (see the "path" to command in the Standard Additions dictionary for other locations, such as pictures folder, movies folder, sites folder, desktop folder)
set the defaultDestinationFolder to (path to documents folder)
tell application "Keynote"
activate
try
if playing is true then tell the front document to stop
if not (exists document 1) then error number -128
-- DERIVE NAME FOR NEW FOLDER FROM NAME OF THE FRONT DOCUMENT
set documentName to the name of the front document
if documentName ends with ".key" then ¬
set documentName to text 1 thru -5 of documentName
-- CREATE AN EXPORT DESTINATION FOLDER
-- IMPORTANT: IT’S ADVISED TO ALWAYS CREATE A NEW DESTINATION FOLDER, AS THE CONTENTS OF ANY TARGETED FOLDER WILL BE OVERWRITTEN
tell application "Finder"
set newFolderName to documentName
set incrementIndex to 1
repeat until not (exists folder newFolderName of defaultDestinationFolder)
set newFolderName to documentName & "-" & (incrementIndex as string)
set incrementIndex to incrementIndex + 1
end repeat
set the targetFolder to ¬
make new folder at defaultDestinationFolder with properties ¬
{name:newFolderName}
set the targetFolderHFSPath to targetFolder as string
end tell
-- EXPORT THE DOCUMENT
with timeout of 1200 seconds
export front document as HTML to file targetFolderHFSPath
end timeout
on error errorMessage number errorNumber
display alert "EXPORT PROBLEM" message errorMessage
error number -128
end try
end tell
-- OPEN THE DESTINATION FOLDER
tell application "Finder"
open the targetFolder
end tell
-- VIEW THE PRESENTATION
tell application "Safari"
activate
open file (targetFolderHFSPath & "index.html")
end tell
I am looking for a way to fix this. Currently I am getting the following result:
error "Keynote got an error: No user interaction allowed." number -1713
You've broken one of the basic rules of applescript. Avoid putting tell blocks inside tell blocks. In your case you put a Finder tell block of code inside the Keynote tell block of code. This could cause conflicts and thus errors. I think that's your problem.
Try this with the tell blocks separated...
-- THE DESTINATION FOLDER
-- (see the "path" to command in the Standard Additions dictionary for other locations, such as pictures folder, movies folder, sites folder, desktop folder)
set the defaultDestinationFolder to (path to documents folder)
try
tell application "Keynote"
activate
if playing is true then tell the front document to stop
if not (exists document 1) then error number -128
-- DERIVE NAME FOR NEW FOLDER FROM NAME OF THE FRONT DOCUMENT
set documentName to the name of the front document
if documentName ends with ".key" then ¬
set documentName to text 1 thru -5 of documentName
-- CREATE AN EXPORT DESTINATION FOLDER
-- IMPORTANT: IT’S ADVISED TO ALWAYS CREATE A NEW DESTINATION FOLDER, AS THE CONTENTS OF ANY TARGETED FOLDER WILL BE OVERWRITTEN
end tell
tell application "Finder"
set newFolderName to documentName
set incrementIndex to 1
repeat until not (exists folder newFolderName of defaultDestinationFolder)
set newFolderName to documentName & "-" & (incrementIndex as string)
set incrementIndex to incrementIndex + 1
end repeat
set the targetFolder to ¬
make new folder at defaultDestinationFolder with properties ¬
{name:newFolderName}
set the targetFolderHFSPath to targetFolder as string
end tell
-- EXPORT THE DOCUMENT
with timeout of 1200 seconds
tell application "Keynote"
export front document as HTML to file targetFolderHFSPath
end tell
end timeout
on error errorMessage number errorNumber
display alert "EXPORT PROBLEM" message errorMessage
error number -128
end try
-- OPEN THE DESTINATION FOLDER
tell application "Finder"
open the targetFolder
end tell
-- VIEW THE PRESENTATION
tell application "Safari"
activate
open file (targetFolderHFSPath & "index.html")
end tell
I have an applescript that creates html files and uses textedit to open them:
try
tell application "Finder" to set save_folder to (target of window 1) as alias
on error
set save_folder to path to desktop
end try
set textFile to (choose file name with prompt "Enter file name:" default location save_folder default name "Comment.html") as text
if textFile does not end with ".html" then set textFile to textFile & ".html"
do shell script "touch " & quoted form of POSIX path of textFile
tell application "Finder"
set file type of (textFile as alias) to "html"
set creator type of (textFile as alias) to "sfri"
end tell
tell application "TextEdit"
open file textFile
activate
end tell
The file opens as locked which is a pain. But if i set the filetype and creator to TEXT and ttxt (identifiers for TextEdit) then all is well. I hate to have to give root access to textedit just to edit html files but i guess that's what is needed? I could switch to another text editor I suppose but I am left with the question as to why TextEdit is acting this way with html files?
To open empty HTML file without lock:
First solution: check the "Display HTML files as HTML code instead of formatted text" button in TextEdit preferences.
But you must write HTML code in the document
--
Second solution: write a valid HTML code for an empty HTML file, like this:
set base to "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\"><html><head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><meta http-equiv=\"Content-Style-Type\" content=\"text/css\">
<title></title><meta name=\"Generator\" content=\"Cocoa HTML Writer\"><meta name=\"CocoaVersion\" content=\"1265.21\">
<style type=\"text/css\"></style></head><body></body></HTML>"
try
tell application "Finder" to set save_folder to (target of window 1) as alias
on error
set save_folder to path to desktop
end try
set textFile to (choose file name with prompt "Enter file name:" default location save_folder default name "Comment.html") as text
if textFile does not end with ".html" then set textFile to textFile & ".html"
do shell script "printf " & (quoted form of base) & " > " & quoted form of POSIX path of textFile
tell application "TextEdit"
open file textFile
activate
end tell