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.
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've been developing an "IDE" for custom software that relies on a web browser control to display it's information. The software uses an external file "common.vbs" to access common functions within it's web pages. Sprinkled throughout various web pages within the application is the html tag:
<script LANGUAGE="VBScript" src="common.vbs"></script>
In my application I've tried the following code after importing the code into a resource string:
Dim sPath As String = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)
sPath = Path.Combine(sPath, "common.vbs")
Try
Dim fs As FileStream = File.Create(sPath)
Dim sData As String = My.Resources.common
Dim Info As Byte() = New UTF8Encoding(True).GetBytes(My.Resources.common)
fs.Write(Info, 0, Info.Length)
fs.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
The code doesn't error. When I test (withing the same function) reading the file back, it's contents are returned; I know the file is being written, but when the any of the pages load with the above HTML tags, I get an error from the page:
An error has occurred in the script on this page
Line: 0
Char: 0
Error: Script error
Code: 0
URL: about:common.vbs
The file I'm trying to "include" contains only functions like:
Function DoHelp
window.external.DoHelp "", "", -1, -1
End Function
Function SQLDate( d)
SQLDate = "'" & DatePart("yyyy", d) & "-" & DatePart("m", d) & "-" & DatePart("d", d) & "'"
End Function
Function MonitorJob( jobId)
on error resume next
window.external.UIControl.MonitorJob jobId, 0
End Function
Within the application, there are no problems loading and using any of the functions contained within the file. The actual contents of the file is from the application and hasn't been edited in any manor. Any page that I load with my application that has the file included errors out, including simple test pages.
Does anyone have any ideas on how to get the web browsers control recognize a file from within a script's scr tag that's not part of the actual page being loaded and supplied from the hosting application? Editing all of the pages within the source application really isn't an option; may are dynamically created and there is a large number of them.
I've tried to include as much information as I can here, but if you need more info, please feel free to ask!
Thanks, Fred
EDIT / UPDATE:
I've tried writing all of the files I need to "include" into a temp folder, write the calling HTML file to the same folder, then load the web control using a file stream. The results are the same.
EDIT / UPDATE
I must have things wrong; Writing all of the "resource Files" into the temp folder, then writing the resulting HTML page into the same file and finally calling the Navigate method to that temp folder and file results in the page properly loading images and the underlying included VBS file.
Last EDIT / UPDATE
It took me a while to get all of the parts working correctly. Below is the answer that solved the issue.
Part of the problem is how the web control is loaded. Using the document methods circumvents accessing the file system because no base location is used. In order to access items in the file system, the "source page" needs to be in the file system. The next part of the problem was getting the resources into the file system. Below is the code that I used. Important note: for demonstration reasons only, the code for the Temp Path is located inside this routine. It actually resides out side the routine for access in other routines.
Sub UpdateFiles(sData)
'Write all the dependent files into the working folder.
Dim sLocalPath As String = Path.Combine(Path.GetTempPath, Guid.NewGuid.ToString)
Do While Directory.Exists(TempWorkingPath) Or File.Exists(TempWorkingPath)
sLocalPath = Path.Combine(Path.GetTempPath, Guid.NewGuid.ToString)
Loop
Directory.CreateDirectory(sLocalPath)
File.WriteAllBytes(Path.Combine(sLocalPath, "common.vbs"), System.Text.Encoding.UTF32.GetBytes(My.Resources.common))
Dim aImageList() As String = Split(My.Resources.M3ImageList, vbCrLf)
Dim sImageList As String = Replace(Join(Split(My.Resources.M3ImageList, vbCrLf), ","), "-", "_")
Dim ResourceSet As Resources.ResourceSet = My.Resources.ResourceManager.GetResourceSet(Globalization.CultureInfo.CurrentCulture, True, True)
For Each Dict As DictionaryEntry In ResourceSet.OfType(Of Object)()
If TypeOf (Dict.Value) Is Drawing.Image Then
If InStr(sImageList, Dict.Key, vbTextCompare) > 0 Then
Dim ImageConverter As New ImageConverter
Dim aBytes() As Byte = ImageConverter.ConvertTo(Dict.Value, GetType(Byte()))
Dim sName As String = Replace(Dict.Key.ToString & ".gif", "_", "-")
File.WriteAllBytes(Path.Combine(sLocalPath, sName), aBytes)
End If
End If
Next
File.WriteAllText(Path.Combine(sLocalPath, "index.html"), sData)
End Sub
I used a separate resource file that listed all of the files I needed to drive this routine. One point of note, when I imported all of the GIF's, the -'s were converted into _'s, thus the replace statements. I also used a filter (the Instr command) so that only the items in the list were exported.
I spent a lot of time on this, and I hope others will find it useful!
Fred
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
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