Here's a script that renames a group of files to their new filenames created in excel. It works and it's great! But the problem is if the files do not exist from the group of files the scripts ends and creates an error..
property sourceFolder : "Macintosh HD:Users:COMP1:Desktop:folder1" -- adjust as appropriate
set filenameList to read file "Macintosh HD:Users:COMP1:Desktop:renamethis.txt" using delimiter {return}
set {oldDelims, text item delimiters} to {text item delimiters, tab}
tell application "Finder"
repeat with aFile in filenameList
set codified_name to (sourceFolder & ":" & (text item 1 of aFile)) as string
set real_name to text item 2 of aFile
set name of file codified_name to real_name
end repeat
end tell
I asked around for some help and they gave me this code to add. The problem is I do not know how and where to add this code and how to define it. I want to be able to create a .txt file that contain the files not renamed instead of stopping the whole script.
try
set name of file codified_name to real_name
on error error_string number error_number
write error_string to log_file_handle starting at eof
end
I don't really know how to code and just rely on posts from the internet and try to tweak them. So if anyone can help me..that would be really really awesome!
Here, this shows how to add a try block into your code above. You needed more code to write to an error log file as well. Should be enough to go on:
property sourceFolder : "Macintosh HD:Users:COMP1:Desktop:folder1" -- adjust as appropriate
set log_file_path to "Macintosh HD:Users:COMP1:Desktop:myErrorLogFile.txt"
set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, tab}
try
set filenameList to read file "Macintosh HD:Users:COMP1:Desktop:renamethis.txt" using delimiter {return}
tell application "Finder"
repeat with aFile in filenameList
set codified_name to (sourceFolder & ":" & (text item 1 of aFile)) as string
set real_name to text item 2 of aFile
set name of file codified_name to real_name
end repeat
end tell
set AppleScript's text item delimiters to oldDelims
on error error_string number error_number
set AppleScript's text item delimiters to oldDelims
set log_file_handle to (open for access file log_file_path with write permission)
write ((current date) as string) & tab & error_string to log_file_handle starting at eof
close access log_file_handle
end try
Related
I have a lot of PDFs that I want to rename. The new name should be a text item of this PDF. I wan t to create this as a folder action to drop PDF in and get them renamed. I've com so far with Automator and AppleScript:
The whole text form the PDF is extracted to a new (temporary) file on desktop "PDF2TXT.rtf" (= variable theFileContents), done with the Automator action to extract text from PDF.
Here comes the code that separates the new name out of this text:
on run {theFileContents}
set od to AppleScript's text item delimiters
set theFileContentsText to read theFileContents as text
set myProjectNo to ""
set theDelimiter to "Projectno. "
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
set theArray to last text item of theFileContentsText
set theDelimiter to " "
set AppleScript's text item delimiters to theDelimiter
set myProjectNo to first text item of theArray
return myProjectNo
set AppleScript's text item delimiters to oldDelimiters
end run
What I need now is the rest to rename the input PDF to "myProjectNo".
Happy for any suggestion. Thanks in advance.
q3player
try this :
tell application "Finder" to set name of foo to bar
the script to save as droplet (= application) :
on run -- you can run the applet it will prompt for files
open {choose file of type "pdf"}
end run
on open (thesefiles) -- you cn drop files on this applet
repeat with theFileContents in thesefiles
-- set od to AppleScript's text item delimiters -- duplicate : same as oldDelimiters
set theFileContentsText to read theFileContents as text
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "Projectno. "
set theArray to last text item of theFileContentsText
set AppleScript's text item delimiters to space
set myProjectNo to first text item of theArray
-- return myProjectNo -- and never come back again ;-)
set AppleScript's text item delimiters to oldDelimiters -- this can't go after the return ( = never called)…
set newPDFname to "Project No" & space & myProjectNo & ".pdf"
tell application "Finder"
display dialog "You really want to renamme this file to " & return & newPDFname & " ?"
set the name of thisFile to newPDFname
end tell
end repeat
end open
everybody, I am trying to make the user enter the name of the movie they want subtitles for and the for apple script to automatically search the subtitle website for the name of the movie they inputted.
To do this all of the spaces in the movie name need to be replaced to the + sign because urls convert spaces to the + sign. The code is not working an im getting the following errors:
Expected “end” but found “on”.
A “(” can’t go after this identifier.
Here is my code;
on run
display dialog "What's the name of the movie?" default answer " " with title "What's the name of the movie?" buttons {"OK"} default button 1
set moviename to text returned of the result
set theText to moviename
set theSearchString to " "
set theReplacmentString to "+"
end findAndReplaceInText(theText, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript's text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
end findAndReplaceInText
goToWebPage("https://rs.titlovi.com/prevodi/?prevod= & thetext")
tell application "Safari"
activate
set URL of document 1 to theWebPage
end tell
end goToWebPage
end run
Thank you in advance.
Functions (handlers, in AppleScript speak) may not be nested in AppleScript. So you need to either move findAndReplaceInText and goToWebPage outside of on run, or merge their functionality into on run without using handlers.
Handlers begin with on handlerName and end with end handlerName; you have findAndReplaceInText beginning and ending with end findAndReplaceInText.
Here’s how it might work after separating the handlers:
on run
display dialog "What's the name of the movie?" default answer " " with title "What's the name of the movie?" buttons {"OK"} default button 1
set moviename to text returned of the result
set moviename to findAndReplaceInText(moviename, " ", "+")
goToWebPage("https://rs.titlovi.com/prevodi/?prevod=" & moviename)
end run
on findAndReplaceInText(thetext, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of thetext
set AppleScript's text item delimiters to theReplacementString
set thetext to theTextItems as string
set AppleScript's text item delimiters to ""
return thetext
end findAndReplaceInText
on goToWebPage(theWebPage)
tell application "Safari"
activate
set URL of document 1 to theWebPage
end tell
end goToWebPage
I’ve verified this code in Safari on Mac OS X 10.14.6.
I am trying to make a simple counter that adds one to the count every time the script is run. I have tried using a property but that doesn't work because it resets whenever the script is edited or the computer is turned off. Here is the code I have that I took from here.
set theFile to ":Users:hardware:Library:Scripts:Applications:LightSpeed:" & "CurrentProductCode.txt"
open for access theFile
set fileContents to read theFile
close access theFile
set counter to fileContents as integer
on add_leading_zeros(counter, max_leading_zeros)
set the threshold_number to (10 ^ max_leading_zeros) as integer
if counter is less than the threshold_number then
set the leading_zeros to ""
set the digit_count to the length of ((counter div 1) as string)
set the character_count to (max_leading_zeros + 1) - digit_count
repeat character_count times
set the leading_zeros to (the leading_zeros & "0") as string
end repeat
return (leading_zeros & (counter as text)) as string
else
return counter as text
end if
end add_leading_zeros
add_leading_zeros(counter, 6)
open for access newFile with write permission
set eof of newFile to 0
write counter + 1 to newFile
close access newFile
With this I get the error:
Can’t make ":Users:hardware:Library:Scripts:Applications:LightSpeed:CurrentProductCode.txt" into type file.
If I add "set theFile to theFile as alias" after the first "open for access theFile" it gets a little further in the code, but gets another error:
Can’t make "1776" into type integer.
And now I am out of ideas. I have googled all over the place haven't found anything that works for me. Thanks
I like to use script objects to store data:
set thePath to (path to desktop as text) & "myData.scpt"
script theData
property Counter : missing value
end script
try
set theData to load script file thePath
on error
-- On first run, set the initial value of the variable
set theData's Counter to 0
end try
--Increment the variable by 1
set theData's Counter to (theData's Counter) + 1
-- save your changes
store script theData in file thePath replacing yes
return theData's Counter
I have a number written in a text file. Is it possible to add another to the number in the text file then replace the old number with the new one? Heres my code:
set theFile to POSIX path of "Users:Tyler:Documents:File.txt"
open for access theFile
set theFileContents to read theFile
set theNewFile to run script theFileContents + 1
tell application "TextEdit"
set eof of theFileContents to 0
write theNewFile to theFileContents
if i understand your question correctly you just want to have a text file with a string and convert that string to a number then increase the value of the number by 1 write it back to the file as a string and save it boom done right ? let me know if this is what you meant.
set docsFolder to path to documents folder
set afile to "" & docsFolder & "File.txt"
open for access file afile with write permission
set oldNum to read file afile
set newString to ((oldNum as number) + 1) as string
set eof file afile to 0
log newString
write newString to file afile starting at eof
close access file afile
I made was making this applescript to launch apps with a note:
tell application "Notes"
if exists note starts with applaunch then
set LCommands to {"Launch", "Open"}
repeat with y from 1 to count LCommands
set applaunch to (item y of LCommands)
set AppleScript's text item delimiters to applaunch
set myApp to text items 2 thru 1 of note
set AppleScript's text item delimiters to {""}
set myApp to myApp as text
if y = 1 or y = 2 then
tell application myApp to launch
end if
end repeat
delete note starts with applaunch
end tell
and returns the error "the variable applaunch is not defined" but i defined it. what to do?
You reference applaunch in line 2, but you don't define it until line 5.
Also, your code example is missing the end if that goes with if exists note starts with applaunch then.
You can try something along these lines:
set LCommands to {"Launch ", "Open "}
tell application "Notes"
repeat with aCommand in LCommands
set aCommand to (contents of aCommand)
set myNotes to (notes whose name begins with aCommand)
repeat with aNote in myNotes
set aNote to contents of aNote
set noteName to aNote's name
set AppleScript's text item delimiters to aCommand
set myApp to text items 2 thru -1 of noteName
set AppleScript's text item delimiters to {""}
set myApp to myApp as text
-- If you need to work with the Note's content as plain text
--set noteBody to do shell script "echo " & (quoted form of (aNote's body as text)) & " | textutil -stdin -convert txt -stdout "
my launchDelete(aNote, myApp)
end repeat
end repeat
end tell
on launchDelete(theNote, theApp)
try
tell application theApp to launch
tell application "Notes" to delete theNote
end try
end launchDelete