Directing an app to run off of a different script in AppleScript. - applescript

I'm making an applescript to help with school. You type the subject you wish to access, and then it opens that folder (inside the app). However, some subjects have ebooks, so I want there to be another dialog box asking you wether you want to open the ebook or the folder. Unfortunately, a display dialog can't branch off of another display dialog.
So far, the only way I've found around this is to direct the app to run off of another script (in the "Scripts" folder of the app). I've tried
tell application "Script Editor" to run script (path to me as sting) & "Contents:Resources:Scripts:Subject.scpt"`
But it didn't work. Sorry if I'm barking up the wrong tree and seem completely stupid. Thanks

As far as I understood what you're trying to achieve, the following script would work.
set listOfSubjects to {"Math", "Biology", "History", "Languages"}
set subjectsWithEbook to {"Biology", "History"}
choose from list listOfSubjects with prompt "Select the subject"
set subject to item 1 of result
set openEbook to false
repeat with subjectItem in subjectsWithEbook
if subjectItem contains subject then
choose from list {"Open folder", "Open Ebook"} with prompt "What to open?"
set openType to item 1 of result
if openType contains "Open Ebook" then
set openEbook to true
end if
exit repeat
end if
end repeat
if openEbook then
--Open ebook
else
--Open folder
end if

I am guessing that you want to type the subject name? Here is an example that uses dialog boxes.
set subjectFolderPath to (path to me as string) & "Contents:Resources:Subjects:"
set subjectsWithEbook to {"English", "Spanish"}
display dialog "What subject would you like to access?" default answer ""
set theSubject to text returned of the result
if theSubject is in subjectsWithEbook then
display dialog "Would you like to open the Folder or the Book?" buttons {"Folder", "Book"} default button 1
set toOpen to button returned of the result
if toOpen is "Book" then
-- tell app "Preview" to open ???
beep
else
tell application "Finder" to open folder (subjectFolderPath & theSubject & ":")
end if
else
tell application "Finder" to open folder (subjectFolderPath & theSubject & ":")
end if
You might also want to have a list of subjects that are allowed and error check for them.

Related

Delete all files in one folder via Apple Script

I worked out two scripts to do that, but none of them is working (I even don't get shown the "on error" message. Maybe it's because of the pre-defined folder as a variable? As for simplicity and a cleaner code I didn't want to write the complete path again, so I used that folder variable. In case it is relevant, the DataHubFolder variable was defined like this earlier: set DataHubFolder to cloudDocs & "Current Projects:Data Hub:"
Here are the codes I used:
Code #1:
try
tell application "Finder"
set f to every file ¬
of folder ¬
of DataHubFolder & "Distrokid:"
delete f
end tell
on error
display dialog ("Error. Couldn't move the file") buttons {"OK"}
end try
Code #2:
try
tell application "Finder"
delete (every item of folder (DataHubFolder & "Distrokid:"))
end tell
on error
display dialog ("Error. Couldn't move the file") buttons {"OK"}
end try
Would be so happy about some tips.
Thanks a lot.

Apple Script runs more than once when file is opened

I have a piece of code which is used as a folder action and constantly watches the folder. When a new file is placed in the folder, the script runs and outputs a dialog box. The user can either open or print the file from the dialog box. However, when I select the open button, the code then produces the dialog box again when the a new file hasn't been opened. This only occurs when the file is opened and not when it is printed.
Can anyone help? Code is below
on adding folder items to theAttachedFolder after receiving theNewItems
set filepath to theNewItems as string
if filepath contains "HA" then
set theDialogText to "HA is in file name"
do shell script "afplay '/System/Library/Sounds/Submarine.aiff'"
display dialog theDialogText buttons {"Dismiss", "Print", "Go to "} default button "Go to order" with icon note
if result = {button returned:"Go to"} then
tell application "Finder"
open file filepath
end tell
else if result = {button returned:"Print"} then
tell application "Shelf Label Printer"
activate
print filepath
quit
end tell
display dialog "Printed" with icon note
end if
if filepath contains "OG" then
set theDialogText to "OG is in file name"
do shell script "afplay '/System/Library/Sounds/Submarine.aiff'"
display dialog theDialogText buttons {"Dismiss", "Print", "Go to"} default button "Go to order" with icon note
if result = {button returned:"Go to"} then
tell application "Finder"
open file filepath
end tell
else if result = {button returned:"Print"} then
tell application "Shelf Label Printer"
activate
print filepath
quit
end tell
display dialog "Printed" with icon note
Edit: Mojave is running on the iMac in question.
There were a few problematic points in this code, any of which could produce odd behavior, so I cleaned it up so that it works correctly on my machine. New code first, then bullet points on what I changed...
on adding folder items to theAttachedFolder after receiving theNewItems
repeat with thisItem in theNewItems
set filepath to POSIX path of thisItem as string
if filepath contains "HA" or filepath contains "OG" then
set theDialogText to "HA or OG is in file name"
do shell script "afplay '/System/Library/Sounds/Submarine.aiff'"
tell application "System Events"
display dialog theDialogText buttons {"Dismiss", "Print", "Go to"} default button 3 with icon note
end tell
if result = {button returned:"Go to"} then
tell application "System Events"
open file filepath
end tell
else if result = {button returned:"Print"} then
tell application "Shelf Label Printer"
activate
print filepath
quit
end tell
display dialog "Printed" with icon note
end if
end if
end repeat
end adding folder items to
"after receiving" always gives a list of aliases, even if it's a single item, so we should loop through the list rather than trying to convert it directly to string.
You duplicated the same code for 'HA' and 'OG' files, so I merged them
Your Display Dialog cited the default button as "Go to order" when your buttons were "Dismiss", "Print", "Go to". That was throwing an error (Display Dialog wants an exact match to one of the buttons). I replaced that with the index 3.
Folder Actions and the Finder do not always play well together, so I switched to the System Events app for the dialogs and the file open procedure.
This should work properly now...

Error with AppleScript - Integer?

I have been making a simple script for my sister, but it does not work. It calls for an integer? I did not specify it in my program, but it calls it anyway...?
My Script:
activate
display dialog "Click Start to start importing your own kindle books!" with title "Kindle Book Uploader by Jeremy Zhang" buttons
{"Cancel", "Start"} default button "Start"
property documentFolder : "documents"
tell application "Finder" to (get name of every disk whose ejectable is true)
try
set kindleLocation to ¬
((choose from list result with prompt "Select your Kindle from the list:") as text)
end try
try
set bookFiles to ¬
((choose file with prompt ¬
"Select kindle files to import:" of type {"public.html", "public.rtf", "com.microsoft.word.doc",
"public.data.mobi", "public.plain-text", "com.adobe.pdf"} with
multiple selections allowed) as text)
end try
display dialog "Please wait while the application copies the kindle books..." with title "Kindle Book Uploader by Jeremy Zhang"
tell application "Finder"
if not (exists folder documentFolder of kindleLocation) then
make new folder at kindleLocation with properties {name:documentFolder}
end if
end tell
set fullKindlePath to POSIX path of (kindleLocation as alias) & "documents"
tell application "Finder"
move (bookFiles) to fullKindlePath
end tell
display dialog "Process has been done! Please eject your kindle and the files will be on the home screen of your Kindle." with title
"Kindle Book Uploader by Jeremy Zhang"
And the result from running it:
tell current application
activate
end tell
tell application "AppleScript Editor"
display dialog "Click Start to start importing your own kindle books!" with title "Kindle Book Uploader by Jeremy Zhang" buttons
{"Cancel", "Start"} default button "Start"
-- {button returned:"Start"}
end tell
tell application "Finder"
get name of every disk whose ejectable = true
-- {"JEREMY DISK"}
end tell
tell application "AppleScript Editor"
choose from list {"JEREMY DISK"} with prompt "Select your Kindle from the list:"
-- {"JEREMY DISK"}
choose file with prompt "Select kindle files to import:" of type {"public.html", "public.rtf", "com.microsoft.word.doc",
"public.data.mobi", "public.plain-text", "com.adobe.pdf"} with
multiple selections allowed
-- {alias "Macintosh HD:Users:JeremyZhang:Downloads:5 ETS SAT S.pdf"}
display dialog "Please wait while the application copies the kindle books..." with title "Kindle Book Uploader by Jeremy Zhang"
-- {button returned:"OK"}
Result:
error "Canât make \"documents\" into type integer." number -1700 from "documents" to integer
What am I doing wrong? Can you please correct me?
AppleScript Editor V: 2.5 (138)
AppleScript 2.2.3
It's a somewhat misleading error. It's telling you that documentFolder is the wrong type. But there are a couple other issues with the script, too. This version should work -- see comments, but try running this as is (I removed the option-return continuation character you were using; I suspect it doesn't translate well on this forum) :
activate
display dialog "Click Start to start importing your own kindle books!" with title "Kindle Book Uploader by Jeremy Zhang" buttons {"Cancel", "Start"} default button "Start"
property documentFolder : "documents"
tell application "Finder" to (get name of every disk whose ejectable is true)
try
set kindleLocation to ((choose from list result with prompt "Select your Kindle from the list:") as text)
--display dialog class of kindleLocation -- class is text
end try
try
set bookFiles to ((choose file with prompt "Select kindle files to import:" of type {"public.html", "public.rtf", "com.microsoft.word.doc", "public.data.mobi", "public.plain-text", "com.adobe.pdf"} with multiple selections allowed) as text)
end try
display dialog "Please wait while the application copies the kindle books..." with title "Kindle Book Uploader by Jeremy Zhang"
tell application "Finder"
--coerce kindleLocation to alias
if not (exists folder documentFolder of alias kindleLocation) then
make new folder at kindleLocation with properties {name:documentFolder}
end if
end tell
--don't use posix ... but you already have variables to build this
--set fullKindlePath to (kindleLocation) & ":documents:"--could've done this, but you don't need this, you can use same construction as above
tell application "Finder"
move (bookFiles) to folder documentFolder of alias kindleLocation
end tell
display dialog "Process has been done! Please eject your kindle and the files will be on the home screen of your Kindle." with title "Kindle Book Uploader by Jeremy Zhang"

How to open a folder containing a specific string? (Mac)

We got some folders with project IDs with the following Pattern: x123_projectname.
I use Alfred in my workflow and I need to find and open a specific folder by the ID.
It is possible to run applescripts from Alfred. I'm new to applescript and google helped me to create this this:
set theString to "/path/to/folder/containing/projects/"
display dialog "What is the ID?" default answer ""
set theID to the text returned of
(display dialog "What is the ID?" default answer "")
tell application "Finder"
activate
set x to theString & "theID" as alias
open x
end tell
but it didn't work - do you have any hints for me?
You have a couple problems with your script but in general you have the right idea. The main problem is that applescript doesn't work with slash delimited paths. Applescript paths are colon delimited. You can convert from slash to colon delimited using the "posix file" command. Give this a try. Good luck.
set theString to "/path/to/folder/containing/projects/"
display dialog "What is the ID?" default answer ""
set theID to the text returned of result
set macString to POSIX file theString
tell application "Finder"
activate
set theResult to every folder of macString whose name contains theID
open theResult
end tell

Applescript studio - how do I get every control in a window

I'm trying to enable or disable all the control in a window as the programme changes from interactive to non-interactive mode. How can I ask a window to give me all its contents?
every control of window "mainWindow"
doesn't work, nor does
contents of window "mainWindow"
Actually, I haven't been able to find any good documentation for interacting with menu items from interface builder at all. Things like how to set the contents of popups, and buttons and so on.
thanks
The way I do it at the moment is:
property onlineControls: {"maxLength", "speed", "accelerationSlider", "accelerationField", "showInfo"} --and so on, listing all the controls by name
on enableControls(theList, enableState)
tell window "mainWindow"
repeat with theControl in theList
set the enabled of control theControl to enableState
end repeat
end tell
enableControls(onlineControls, true)
I've made several lists of controls tht get turned on or off depending on the state the programme is in. But it has to be hard coded, which I don't see as being the best way.
tell application "System Events"
tell process "Adium"
get entire contents of window 1
end tell
end tell
This script will give you as result all contents of front window of Adium: butons of window, tool bars of window, buttons of tool bars, etc. Enjoy =]
I haven't been able to find a way to get all the controls in a window, but here's an example of interacting with the menu of a popup button:
tell menu of popup button "somePopupButton" of window "mainWindow"
delete every menu item
repeat with i in someItems
make new menu item at end of menu items ¬
with properties {title:i, enabled:true}
end repeat
end tell
Is the same script as "BoB1990" with the possibility of getting back the information given by get entire contents of window in a string of whom you can observe or modify all the items listed :
tell application "System Events" to tell process "Adium"
set this_info to {}
try
display alert ((get entire contents of window (x as integer)))
on error errMsg set theText to errMsg
set this_info to do shell script " echo " & theText & " | sed 's#System Events got an error: Can’t make ##g;s# into type string.##g'"
end try
set info to {}
set info to do shell script " echo " & this_info
display alert (info)
end tell

Resources