Please excuse my less-than-elegant scripting ability, but the script is working fine when invoked within the script editor itself. When I save it as an application however, the icon doesn't show that it's a droplet and does not work as such. Any help is greatly appreciated!
try
set destinationFolder to "Mercury:F1_PropertyLogos:"
tell application "Finder" to set logoFileName to name of item 1 of (get selection)
end try
set file_name to logoFileName
set file_name to remove_extension(file_name)
on remove_extension(this_name)
if this_name contains "." then
set this_name to ¬
(the reverse of every character of this_name) as string
set x to the offset of "." in this_name
set this_name to (text (x + 1) thru -1 of this_name)
set this_name to (the reverse of every character of this_name) as string
end if
return this_name
end remove_extension
tell application "Finder"
set selected_items to selection
set theFolder to "Mercury:F1_PropertyLogos:"
repeat with x in selected_items
move x to theFolder
end repeat
end tell
tell application "QuarkXPress"
set mypath to "Mercury:F1_Layouts:"
set myfile to file_name
set extension to ".qxp"
set logoFolderPath to "Mercury:F1_PropertyLogos:"
set myLogoFile to file_name
set myLogoExtension to ".psd"
set myLogo to (logoFolderPath & myLogoFile & myLogoExtension)
open file (mypath & myfile & extension)
set selected of picture box "Logo" of spread 1 of document 1 to true
set image 1 of picture box "Logo" of spread 1 of document 1 to file myLogo
set bounds of image 1 of picture box "Logo" of spread 1 of document 1 to proportional fit
end tell
end
To deal with items dropped onto the application, you will need to add an open handler that receives a list of the dropped items (even if there is only one), for example:
on open theDroppedItems
-- whatever
end open
You should probably rearrange your code to place your main statements into a handler (or handlers) that can be called from multiple places, since double-clicking on the application will call the run handler instead.
Related
I am writing a script to go to the NYT website on Corona, get the US data, extract numbers (total, death), and to send me a notification. I am close, but when I extract numbers and display them, they are put together (ie 700021 instead of 7000,21). My question is:
How do I extract the numbers so that they are delineated?
Here is the code:
set theURL to "https://www.nytimes.com/interactive/2020/world/coronavirus-maps.html?action=click&pgtype=Article&state=default&module=styln-coronavirus&variant=show®ion=TOP_BANNER&context=storyline_menu"
tell application "Safari" to make new document with properties {URL:theURL}
tell application "System Events"
repeat until exists (UI elements of groups of toolbar 1 of window 1 of application process "Safari" whose name = "Reload this page")
delay 0.5
end repeat
end tell
to getInputByClass(theClass, num)
tell application "Safari"
set input to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerText;" in document 1
end tell
return input
end getInputByClass
set myVar to getInputByClass("g-body ", 5)
on returnNumbersInString(inputString)
set s to quoted form of inputString
do shell script "sed s/[a-zA-Z\\']//g <<< " & s
set dx to the result
set numlist to {}
repeat with i from 1 to count of words in dx
set this_item to word i of dx
try
set this_item to this_item as number
set the end of numlist to this_item
end try
end repeat
return numlist
end returnNumbersInString
set theNums to returnNumbersInString(myVar) as text
display notification "COVID-19 UPDATE" subtitle theNums sound name "glass"
tell application "Safari"
close its front window
end tell
You are getting a list of numbers from the returnNumbersInString handler, but just coercing the list to text doesn't normally provide any kind of formatting. One solution would be to use text item delimiters to specify the text to use when joining the list items. For example, when converting to text for the notification you could do something like:
set tempTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
set theNums to returnNumbersInString(myVar) as text
set AppleScript's text item delimiters to tempTID
Similar to your other question I helped you with, the target data is already in a table and as such I'd use the table data to get the information as its structure layout is not likely to change where target 'g-body ' of 5 may not always be the United States.
I get my data a little different way:
set theURL to "https://www.nytimes.com/interactive/2020/world/coronavirus-maps.html?action=click&pgtype=Article&state=default&module=styln-coronavirus&variant=show®ion=TOP_BANNER&context=storyline_menu"
tell application "Safari" to make new document with properties {URL:theURL}
tell application "System Events"
repeat until exists ¬
(UI elements of groups of toolbar 1 of window 1 of ¬
application process "Safari" whose name = "Reload this page")
delay 0.5
end repeat
end tell
tell application "Safari" to tell document 1 to set CountriesTable to ¬
do JavaScript "document.getElementsByClassName('svelte-f9sygj')[0].innerText;"
tell application "Safari" to close its front window
set awkCommand to ¬
"awk '/United States/{print $3,\"Cases &\",$4,\"Deaths\"}'"
set notificationMessage to ¬
do shell script awkCommand & "<<<" & CountriesTable's quoted form
display notification notificationMessage subtitle "US COVID-19 UPDATE" sound name "glass"
NOTE: The code used to determine when the page in Safari has finished loading works in macOS Mojave and later, however, for macOS High Sierra and some earlier versions, add the words buttons of in front of UI elements ... in the repeat until exists ¬ ... code.
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.
Images are key to my work.
I am trying to find a quick way to automate image insertion to keynote. The script I am drafting is to get the last created/downloaded image, and insert it into the current slide on Keynote as a replacement of the master's object placeholder.
Error from applescript:
"Keynote got an error: Can’t set file name of slide 2 of document id \"B1054797-9A07-4642-AE79-166D1DE72674\" to alias." number -10006 from file name of slide 2 of document id "B1054797-9A07-4642-AE79-166D1DE72674" to alias
set myFolder to "/Users/Mingyu/Desktop/UpperEchelon"
tell application "Finder" to set latestFile to item 1 of (sort files of (POSIX file myFolder as alias) by creation date) as alias
tell application "Keynote"
activate
tell the front document
tell the current slide
set thisPlaceholderImageItem to item 1
set file name of thisPlaceholderImageItem to ¬
alias
end tell
end tell
end tell
I expect the script will insert last download image in the folder "UpperEchelon" to a current open slide in Keynote
Two issues:
You have to set thisPlaceholderImageItem to image 1 rather than to item 1
You have to set the file name to latestFile rather than just to alias. That's what the error is telling you.
And if the folder is a subfolder of the desktop there is a much shorter syntax
set myFolder to "UpperEchelon"
tell application "Finder" to set latestFile to item 1 of (sort files of folder myFolder by creation date) as alias
tell application "Keynote"
activate
tell the front document
tell the current slide
set thisPlaceholderImageItem to image 1
set file name of thisPlaceholderImageItem to latestFile
end tell
end tell
end tell
I have an AppleScript that works perfectly in finder. It creates a folder named with the days date and has a shortcut key connected to it.
But it does not work when in another app, in the save window. Can you please help.
Here is the code I have.
tell application "Finder"
try
if exists Finder window 1 then
set thisPath to (the target of the front window) as alias
else
set thisPath to (path to desktop)
end if
on error
return
end try
end tell
set x to my the_perfect_datestring()
if x is not "-ERROR" then
set fullPath to thisPath & x as text
tell application "Finder"
try
--activate
if not (exists fullPath) then
set y to make new folder at thisPath with properties {name:x}
end if
activate
end try
end tell
end if
on the_perfect_datestring()
try
set cd to (the current date)
set the_year to year of (cd) as number
set the_month to month of (cd) as number
set the_day to day of (cd) as number
if the_month < 10 then set the_month to "0" & the_month
if the_day < 10 then set the_day to "0" & the_day
return ((the_year & "-" & the_month & "-" & the_day) as string)
on error
return "-ERROR"
end try
You cannot access the settings in a Save dialog box with the Finder AppleScript terminology.
The only way with AppleScript is System Events and GUI scripting. The script syntax depends strongly on the particular UI.
To work with save dialogs in other applications, you would need to change your workflow a bit. The save dialog works with text (the file/folder name), so you can create a text service to paste in the desired string.
To create a service, start up the Automator application and select the Service (Quick Action) document - the workflow will have settings that determine what kind of input it will accept, for example:
Workflow receives current text in any application
Next, check the Output replaces selected text box - this will replace selected text with the output of your workflow.
Drag a Run AppleScript action into the workflow, and completely replace the default contents with a new Run handler and your date string handler, for example:
on run {input, parameters}
set x to the_perfect_datestring()
if x is not "-ERROR" then return x
end run
on the_perfect_datestring()
try
# do your filename stuff
tell (current date) as «class isot» as string
return text 1 thru 10
end tell -- or whatever
on error
return "-ERROR"
end try
end the_perfect_datestring
After you save the workflow, the service should be available any time you select some text (in the right-click context menu).
An equivalent to your original workflow with the Finder would then be to create a new folder, and use the service on the highlighted “untitled folder” name to change it.
I'm using a code to format textedit files for lab data.
I currently am using AppleScript to create a document in TextEdit and adding text to it, but when I try to save it, TextEdit gives me the error "you do not have permission to save as blahblahblah." I have already tried changing the permission on the folder in which I'm saving it in, but I think it may have something to do with it being a file that AppleScript created.
The exact error output is a dialog box from TextEdit
The document “1.txt” could not be saved as “1”. You don’t have permission.
To view or change permissions, select the item in the Finder and choose File > Get Info.
The segments of my code that don't work are
tell application "TextEdit"
make new document with properties {name:("1.txt")}
end tell
--data formatting code here (n is set here)
tell application "TextEdit"
delay 1
close document 1 saving in ("/Users/bo/Desktop/Script Doc/" & (n as string))
set n to n + 1
make new document with properties {name:((n as string) & ".txt")}
delay 1
end tell
I have scoured other questions and I have found the code segments
open for access document 1
close access document 1
but I'm not sure how to implement these / if I even should, and if not, I'm not sure how to fix this issue.
Thanks in advance
Unfortunately, that error message isn't helping much. You are essentially trying to save into a string, which isn't going to work - you need to use a file specifier, for example:
close document 1 saving in POSIX file ("/Users/bo/Desktop/Script Doc/" & n & ".txt")
Note that not everything knows about POSIX paths, in which case you will need to coerce or specify it as in my example.
Saving directly to the Desktop works for me using the latest version of macOS Mojave
property outputPath : POSIX path of (((path to desktop as text) & "Script Doc") as alias)
property docNumber : 1
property docExtension : ".txt"
tell application "TextEdit"
set docName to (docNumber & docExtension) as text
set theText to "Your Desired Text Content"
set thisDoc to make new document with properties {name:docName, text:theText}
close thisDoc saving in POSIX file (outputPath & "/" & (name of thisDoc))
(* IF YOU WANT TO SAVE MULTIPLE FILES WITH CONSECUTIVE NAMES *)
set docNumber to docNumber + 1
set docName to (docNumber & docExtension) as text
set thisDoc2 to make new document with properties {name:docName} -- Removed Text Property This Time
close thisDoc2 saving in POSIX file (outputPath & "/" & (name of thisDoc2))
end tell
TextEdit is sandboxed. You cannot save documents in the standard desktop folder.
An alternative is to hard-code the path to the documents folder in the container of TextEdit.
tell application "TextEdit"
make new document with properties {name:"1.txt"}
end tell
set containerDocumentsFolder to (path to library folder from user domain as text) & "Containers:com.apple.TextEdit:Data:Documents:"
tell application "TextEdit"
close document 1 saving in containerDocumentsFolder & (n as string) & ".txt"
set n to n + 1
make new document with properties {name:(n as string) & ".txt"}
end tell
I have a script that changes the icon of a folder with the image you select and it works fine but I want to be able to have the script automatically select the first image of the folder and assign that as the image that will be used to create the icon.
This is what I have so far. It is unable to select the image and gives various error messages depending on what I try. Any help would be greatly appreciated!
set exePath to (path to home folder as text) & "downloads:SetFileIcon"
tell application "Finder"
set theSelection to selection -- This is a list of selected items, even if only 1 item is selected.
set theFile to (item 1 of theSelection) as text -- Get the first item in the list. We make it text so we can convert it to a posix path for SetFileIcon.
set theimage to item 1 of theSelection
set imagePath to (path to desktop folder as text) and theimage
end tell
do shell script quoted form of POSIX path of exePath & " -image " & quoted form of POSIX path of imagePath & " -file " & quoted form of POSIX path of theFile
A couple of issues with your script at first glance. First, you’re never asking for the contents of the folder. You set theFile to item 1 of theSelection and then you set theimage to item 1 of theSelection. I expect you meant to set theimage to item 1 of the document files of theFile. Note that theFile is actually the selected folder.
Second, “and” is a boolean operation; to concatenate text in AppleScript, use “ & ”. Thus, even if theimage contained the image’s name, you would need to use & theimage.
However, since you are asking the Finder for the information, the Finder already knows the full path to any particular file. So, once you get an image, you can ask the Finder for the path directly.
tell application "Finder"
-- This is a list of selected items, even if only 1 item is selected.
set theSelections to the selection
-- This code assumes that the first selected item is a folder
set theFolder to the first item of theSelections
-- we want the first document that is also an image
set theImages to the document files of theFolder whose kind contains "image"
set theImage to item 1 of theImages as text
-- and we want it's path
set imagePath to POSIX path of theImage
end tell