so i written this code so far, it works very well, the only issue is, calling the code twice results with an error «script» doesn’t understand the Remi message. (-1708)
Whats to point here and how can i unset the handler after it was triggered?
The Code:
my Remi()
on Remi()
set cD to (current date)
tell application "Reminders"
--set output to name of reminders
if (count of (reminders whose completed is false)) > 0 then
set output to ""
set todoList to name of reminders whose completed is false
repeat with itemNum from 1 to ((count of (reminders whose completed is false)))
try
set Remi to item itemNum of reminders
set remiT to due date of Remi
set tim to time string of remiT
set dD to date string of remiT
set nN to name of Remi
if remiT ≤ cD then
set val to (tim & " - " & nN & " $$" & dD & "/ENDE")
set output to (output & val & return)
end if
end try
end repeat
else
set output to "No reminders available"
end if
end tell
return output
end Remi
Thx for help
I see what is causing the problem. You have a variable "Remi" inside the "Remi()" handler. I guess you can't do that! So either change the name of the variable or the name of the handler and you should be good.
The problem is caused by the set Remi to item itemNum of reminders statement which changes the value of the remi global variable from a handler to whatever `item itemNum of reminders' turns out to be. The second time through, when you ask AppleScript to call the Remi handler, it is no longer handler and so the call fails.
You can change your code to not alter the global Remi variable, or you can declare Remi local within the handler using a local Remi statement. This protects the global version of Remi from alteration.
Related
I'm writing a handler that will focus on a Safari tab when the window name starts with a substring. Why is this script not working? I've verified the text and it returns true when I isolate the strings (e.g. "localhost:8080..." starts with "localhost" -- #> true.
log focusTabStartingWithName("localhost")
to focusTabStartingWithName(theNameStarting as text)
tell application "Safari"
set found to false
repeat with nextWindow in every window
set tabList to every tab of nextWindow
try
repeat with nextTab in tabList
set tabName to (name of nextTab) as text
log "[" & tabName & "]:" & theNameStarting & ":" & (tabName starts with theNameStarting)
-- #> localhost:8080/somewebapp:localhost:false INCORRECT!!!
set found to tabName starts with theNameStarting
if found then
tell nextWindow
set current tab to nextTab
set visible to true
end tell
exit repeat
end if
end repeat
on error
log "Error encountered getting the next tab in the list"
end try
if found then exit repeat
end repeat
end tell
return found
end focusTabStartingWithName
Change line 1 to:
log focusTabStartingWithName("http://10.")
Change line 9 to:
set tabName to (URL of nextTab) as text
At your leisure, you can change some of your variable names to more appropriate ones but the above should work as long as the localhost IP begins with '10.'.
I am working to outline a workflow in AppleScript. The script takes the next task I need to do from Omnifocus and requires me to determine if I can do it in 2 minutes or less. If I can, it starts a timer and I want it to wait until I actually do the task. Right now I have a dialog box pop up and I can mark the task complete when I am done with it. Unfortunately, some of the tasks I need to do are in Omnifocus and I can't do anything in Omnifocus with the dialog box open.
I would like to avoid using the dialog box so I can work in Omnifocus while the script is running. I'd like to be able to tell the script I'm done so it can stop the timer, tell me how long it took to do the task and then go on to mark the task complete in Omnifocus. I initially thought the best way to do this would be by entering a key combination. After a bit of research, I don't think I can do this in AppleScript. I am open to any idea of how to allow me to work in the middle of my script and then tell the program I am done with the task.
Here's my code:
on run {}
with timeout of (30 * 60) seconds
tell application "OmniFocus"
activate
end tell
tell application "OmniFocus"
tell default document to tell front document window
set perspective name to "Daily Wrap-Up"
tell content to set TreeList to (value of first leaf)
repeat with ListItem in TreeList
set ProjectName to name of containing project of ListItem as text
set TaskName to " - " & name of ListItem
set NoteName to " - " & note of ListItem
display dialog "The task is:" & return & ProjectName & TaskName & NoteName & return & "Can you do this in 2 minutes or less?" buttons {"Yes", "No"} default button "Yes"
set Button_Returned to button returned of result
if Button_Returned = "Yes" then
say "Get to work!"
set T1 to minutes of (current date)
set T1s to seconds of (current date)
display dialog "Click when done." buttons {"Complete", "Cancel"} default button "Complete"
set Button_Returned to button returned of result
if Button_Returned = "Complete" then
set T2 to minutes of (current date)
set T2s to seconds of (current date)
set TT_ to ((T2 * 60) + T2s) - ((T1 * 60) + T1s)
say "that took you" & TT_ & "seconds to complete"
display dialog ProjectName & TaskName & NoteName buttons {"Complete", "Defer", "Cancel"} default button "Complete"
set Button_Returned to button returned of result
if Button_Returned = "Complete" then
mark complete ListItem
tell application "OmniFocus"
compact default document
end tell
else if Button_Returned = "Defer" then
display dialog "Defer for how long (in minutes)?" default answer "60"
set TimeAdd to text returned of result
set defer date of ListItem to ((current date) + (TimeAdd * minutes))
tell application "OmniFocus"
compact default document
end tell
else if Button_Returned = "Cancel" then
tell application "OmniFocus"
compact default document
end tell
else if Button_Returned = "No" then
tell application "OmniFocus"
compact default document
end tell
end if
else if Button_Returned = "No" then
display dialog "Breakdown task."
set perspective name to "Projects"
end if
end if
end repeat
end tell
end tell
end timeout
end run
Thanks in advance for any help.
I don't have OmniFocus on my machine, so I can't properly compile this much less test it, but in vanilla AppleScript you can do something like the following:
global start_time, end_time, TreeList, current_task_index, TaskName, NoteName
on run
tell application "OmniFocus"
tell default document to tell front document window
set perspective name to "Daily Wrap-Up"
tell content to set TreeList to (value of first leaf)
end
end
set current_task_index to 1
beginTask()
end
on reopen
-- inserted try block to aid debugging
try
set end_time to (current date)
set elapsed_time to end_time -start_time
say "that took you " & elapsed_time & " seconds to complete"
display dialog ProjectName & TaskName & NoteName buttons {"Complete", "Defer", "Cancel"} default button "Complete"
set Button_Returned to button returned of result
if Button_Returned = "Complete" then
mark complete ListItem
tell application "OmniFocus"
compact default document
end tell
else if Button_Returned = "Defer" then
display dialog "Defer for how long (in minutes)?" default answer "60"
set TimeAdd to text returned of result
set defer date of ListItem to ((current date) + (TimeAdd * minutes))
tell application "OmniFocus"
compact default document
end tell
else if Button_Returned = "Cancel" then
tell application "OmniFocus"
compact default document
end tell
else if Button_Returned = "No" then
tell application "OmniFocus"
compact default document
end tell
end if
else if Button_Returned = "No" then
display dialog "Breakdown task."
set perspective name to "Projects"
end if
set current_task_index to current_task_index + 1
if current_task_index <= count of TreeList then
beginTask()
else
quit
end
on error errstr number errnum
display alert "Error " & errnum & ": " & errstr
end try
end
on idle
(*
you can use this handler if you want the app to give you a countdown, or
announce a time limit, or anything that needs doing while you're working on the task
*)
end
on beginTask()
tell application "OmniFocus"
tell default document to tell front document window
set perspective name to "Daily Wrap-Up"
set ListItem to item current_task_index of TreeList
set ProjectName to name of containing project of ListItem as text
set TaskName to " - " & name of ListItem
set NoteName to " - " & note of ListItem
display dialog "The task is:" & return & ProjectName & TaskName & NoteName & return & "Can you do this in 2 minutes or less?" buttons {"Yes", "No"} default button "Yes"
set Button_Returned to button returned of result
if Button_Returned = "Yes" then
say "Get to work!"
set start_time to (current date)
end if
end tell
end tell
end
Copy this into the script editor, debug it, and save it as an application with the 'Stay open after run handler' checkbox checked. Operation is as follows:
When OmniFocus is ready, run this script application. It will prompt you to begin the first task.
When you have finished the first task, double-click the script application icon again to invoke the reopen handler. The script will present you with the elapsed time for the first task, give you the choices you outlined, and then prompt you to begin the second task.
After the last task is complete, the script will automatically quit.
The global variable at the beginning allow necessary data to be passed between handlers as the script progresses.
If you want something more complex — e.g., a floating window or menu bar item that gives you more fine-grained control — then you'll need to start using ASOC to build that up. But that's fine-tuning; this should give you a general structure.
A user of a script of mine which communicates via AppleScript with iPhoto is getting this error, which I can't reproduce: 918:955: execution error: iPhoto got an error: "4.294967323E+9Mahabalipuram" doesn’t understand the “write” message. (-1708)
The AppleScript that produces the error is:
set nul to character id 0
set text item delimiters to nul
set albumsFile to "/Users/[user]/Downloads/blah.blah"
set fp to open for access (POSIX file albumsFile) with write permission
tell application "iPhoto"
repeat with anAlbum in albums
if anAlbum's type is regular album then
set albumName to anAlbum's name
if albumName is not "Last Import" then
set albumPhotoIds to (id of every photo of anAlbum) as Unicode text
if length of albumPhotoIds is greater than 0 then
set currentAlbum to anAlbum
repeat while currentAlbum's parent exists
set currentAlbum to currentAlbum's parent
set albumName to currentAlbum's name & " > " & albumName
end repeat
set albumId to anAlbum's id
set albumData to {"", albumId, albumName, ""} as Unicode text
write albumData to fp as Unicode text
write albumPhotoIds to fp as Unicode text
write nul to fp as Unicode text
end if
end if
end if
end repeat
end tell
close access fp
Does anyone have any ideas as to what is going wrong here? There's a bit more background in this Github issue: https://github.com/jawj/iphoto-flickr/issues/7
This may work (untested); it comes up typically with this kind of error. But, as adayzone points out, it is probably best to re-structure the script.
tell me to write albumData to fp as Unicode text
tell me to write albumPhotoIds to fp as Unicode text
tell me to write nul to fp as Unicode text
It's also good to illustrate how tell works (and sometimes "gets in the way")
Write is from StandardAdditions, not iPhoto, so you can't tell iPhoto to write. It would be something along the lines of:
property nul : character id 0
set text item delimiters to nul
set albumsFile to (path to downloads folder as text) & "blah.txt"
tell application "iPhoto"
repeat with anAlbum in albums
if anAlbum's type is regular album then
set albumName to anAlbum's name
if albumName is not "Last Import" then
set albumPhotoIds to (id of every photo of anAlbum)
if length of albumPhotoIds is greater than 0 then
set currentAlbum to anAlbum
repeat while currentAlbum's parent exists
set currentAlbum to currentAlbum's parent
set albumName to currentAlbum's name & " > " & albumName
end repeat
set albumId to anAlbum's id
set albumData to {"", albumId, albumName, ""} as Unicode text
my writeIt(albumsFile, albumData, albumPhotoIds)
end if
end if
end if
end repeat
end tell
on writeIt(albums_File, album_Data, album_PhotoIds)
try
set fp to open for access albums_File with write permission
write album_Data to fp
write album_PhotoIds to fp
write nul to fp
close access fp
on error
try
close access fp
end try
end try
end writeIt
This code will duplicate a Microsoft Outlook message to the drafts folder:
tell application "Microsoft Outlook"
...
-- find the template give an ID
set theTemplate to message id theID
-- duplicate the message
duplicate theTemplate to drafts
...
end tell
I need a reference to the duplicate for additional processing.
Unfortunately, this doesn't work:
...
-- this will create a duplicate
set theDuplicate to (duplicate theTemplate to drafts)
-- produces an error that reads "The variable theDuplicate is not defined."
display dialog (subject of theDuplicate) & " [" & (id of theDuplicate) & "]"
How do I get a reference to the message that was just duplicated? Its ID would be a satisfactory alternative.
There must be a better way but...
--For Testing
set theID to 39110
tell application "Microsoft Outlook"
set oldIds to my getDraftIds()
-- find the template give an ID
set theTemplate to message id theID
--duplicate the message
duplicate theTemplate to drafts
set newIds to my getDraftIds()
set duplicatedMessage to message id (my findNewID(oldIds, newIds))
end tell
on getDraftIds()
set messageIDs to {}
tell application "Microsoft Outlook"
set draftFolders to (every folder whose name = "Drafts")
repeat with dFolder in draftFolders
set messageIDs to messageIDs & id of dFolder's messages
end repeat
end tell
end getDraftIds
on findNewID(oldList, newList)
repeat with mID in newList
if mID is not in oldList then return mID
end repeat
end findNewID
I guess the duplicate method is pretty limited in this regard. I tried the more generic copy method too - the message is copied, but again no ID is returned.
Here's another possible way to do this with no repeat loops:
Create a new, temporary category
Mark the original message with the new category
Duplicate - the duplicate message will also be marked with the category
search for messages marked with the temporary category - you should only get two - the original and the duplicate
Delete the temporary category
Here is the code (rather unfinished):
tell application "Microsoft Outlook"
try
set tempCategory to category "Temporary Category"
on error number -1728
set tempCategory to (make new category with properties {name:"Temporary Category"})
end try
set messageList to selected objects
set origMsg to item 1 of messageList
display dialog "Original Message ID: " & id of origMsg
set msgCat to category of origMsg
set end of msgCat to tempCategory
set category of origMsg to msgCat
duplicate origMsg to drafts
delay 1 -- sigh, it seems to take a bit of time before the category markings are reflected in the spotlight DB
--set msgList to messages whose category contains tempCategory
set currentIdentityFolder to quoted form of POSIX path of (current identity folder as string)
set tempCatMsgs to words of (do shell script "mdfind -onlyin " & currentIdentityFolder & " 'com_microsoft_outlook_categories == " & id of tempCategory & "' | xargs -I % mdls -name com_microsoft_outlook_recordID '%' | cut -d'=' -f2 | sort -u | paste -s -")
if item 1 of tempCatMsgs is (id of origMsg) as text then
set dupMsgId to item 2 of tempCatMsgs
else
set dupMsgId to item 1 of tempCatMsgs
end if
delete tempCategory
display dialog "Original Message ID: " & id of origMsg & return & "Duplicate Message ID: " & dupMsgId
end tell
I thought it would be easier to find messages with a given category using the OL dictionary, but instead had to resort a spotlight search. I'm sure there is a better way to do this.
Also adding a category to a message was harder than I though - again I'm sure this can be done more efficiently.
-- create a temporary folder to hold duplicates
on createFolder(theName)
tell application "Microsoft Outlook"
set theFolder to make new mail folder with properties {name:theName}
end tell
end createFolder
-- find folder by name
on findFolder(theName)
tell application "Microsoft Outlook"
set theFolder to make new mail folder with properties {name:theName}
end tell
end findFolder
-- duplicate the message and get a reference
on duplicateIt(theID)
set theDestination to findFolder("foo bar")
tell application "Microsoft Outlook"
--find the template
set theTemplate to message id theID
-- duplicate it to the temporary location
(duplicate theTemplate to theDestination)
-- get the first item in the folder
set theDuplicate to item 1 of theDestination
-- do something with it
...
end tell
end duplicateIt
Ok Unity3d allows you to set your external script editor in the application preferences. So I want to use applescript to launch my own editor. This applescript has worked pretty well for me so far but I have been unable to jump to the line number.
According to Unity the "line number should be sent through a parameter in an AppleEvent. It should be of typeChar and of keyAEPosition ('kpos') The structure sent through this parameter has the following layout:"
struct TheSelectionRange
{
short unused1; // 0 (not used)
short lineNum; // line to select (<0 to specify range)
long startRange; // start of selection range (if line < 0)
long endRange; // end of selection range (if line < 0)
long unused2; // 0 (not used)
long theDate; // modification date/time
};
"lineNum should be populated with the correct line. The other fields will not be populated with anything than 0 and -1."
So how come I don't see any of this coming through my input? how do I capture this apple event?
My Script:
on run input
set element to item 1 of input
if (element is in {{}, {""}, ""}) then
return
else
tell application "System Events"
set ProcessList to name of every process
if "iTerm" is in ProcessList then
set iterm_running to true
else
set iterm_running to false
end if
log iterm_running
end tell
tell application "iTerm"
activate
if (count terminal) < 1 then
set term to (make new terminal)
else
set term to current terminal
end if
tell term
set create_session to false
try
do shell script ("/usr/local/bin/vim --servername UNITY --remote-send ''")
set create_session to false
on error errorMessage number errorNumber
set create_session to true
end try
if iterm_running then
if create_session then
launch session "Default Session"
activate current session
tell current session
set name to "Unity"
write text "/usr/local/bin/vim --servername UNITY --remote-silent \"$(echo \"/Volumes/" & input & "\" | tr : /)\""
end tell
else
do shell script ("/usr/local/bin/vim --servername UNITY --remote-silent \"$(echo \"/Volumes/" & input & "\" | tr : /)\"")
end if
else
activate current session
tell current session
set name to "Unity"
write text "/usr/local/bin/vim --servername UNITY --remote-silent \"$(echo \"/Volumes/" & input & "\" | tr : /)\""
end tell
end if
end tell
end tell
end if
return input
end run
If you handle the open event, you should see some parameters, including the line number:
on open (x,y)
display dialog x
display dialog y
-- use x and y in your own script
end open