System date validate upon application launch - xcode

I have built an applescript based app in Xcode V3.2.6 under OSX 10.6.7.
I want to have additional code to my application so when the system launched, it will compare the date I set in the application with the system date.
If date within range specified, proceed. If date check is out of range then terminate program immediately.
currently the code looks something like this:
on clicked the Object
if name of theObject = "One" then
try
display dialog "ONE"
end try
else if name of theObject = "two" then
try
display dialog "TWO"
end try
end if
end clicked
on action theObject
end action
One of the very nice users in this fourm post Chuck has posted something. The code works great under apple scripter but not when I pasted into the Xcode.
Can any one tell me what I am doing wrong?
posted by Chuck
set range to 3 * days
set targetDate to (date "Saturday, March 19, 2011 12:00:00 AM")
set currDate to current date
if abs(targetDate - currDate) > range then
display dialog "quit"
else
display dialog "continue"
end if
on abs(n)
if n < 0 then
return -n
else
return n
end if
end abs
Thanks so much!

It's been a long time since I worked with the technology formerly known as AppleScript Studio. :) However, I just created a Cocoa-AppleScript application and I see that the file UntitledAppDelegate.applescript has the following by default:
script UntitledAppDelegate
property parent : class "NSObject"
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
Note the on applicationWillFinishLaunching_ handler and the comment placed there by Xcode. This is where you want to place code that will execute when the program launches. For example, I placed a beep statement in there and the application beeped when it launched. So I'm guessing you could have something like this:
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
set range to 3 * days
set targetDate to (date "Saturday, March 19, 2011 12:00:00 AM")
set currDate to current date
if (currDate - targetDate) > range then
display dialog "quit"
else
display dialog "continue"
end if
end applicationWillFinishLaunching_

Related

"current record" doesn't work: applescripting Filemaker Pro 13 in a loop

I have an filemaker Pro 13 database with around 120 records (it's for a conference). I want to team it up with BBEdit to create individual files for each abstract, thus applescript. Much to my surprise (and despite a lot of web tips on scripting) '[tag:current record]' is not recognised in the script.
The relevant bit is this:
FM Script:
Loop
Perform Applescript
tell application "FileMaker Pro"
activate
set MyFileName to cell "WebAbstractFileName" of table "SelectionProcess"
set MyWebAbstract to cell "WebAbstract" of table "SelectionProcess" as text
end tell
-- (BBEdit bit, which works fine in testing)
Go to Next Record (exit after last)
End Loop
This works fine if I only want to retrieve the first record!
This applescript is set within a filemaker script which loops through the records but the script doesn't care which record it's in.
I've tried adding 'of current record' before the table reference but it then gives me errors (eg error "FileMaker Pro got an error: Object not found." number -1728 from cell "WebAbstractFileName" of current record of table "SelectionProcess") Without 'current record' it works fine, but only gives me the first record.
Here's (roughly) how you could do this in a Filemaker script:
Go to Layout [ “YourTable” ]
# FIND THE RECORDS OF INTEREST
Perform Find [ Restore ]
Go to Record/Request/Page [ First ]
Loop
New Window [ ]
# ISOLATE THE CURRENT RECORD
Show All Records
Omit Record
Show Omitted Only
Set Variable [ $path; Value:Get ( DocumentsPath ) & "someFolder/" & YourTable::Somefield & ".html" ]
Export Records [ No dialog; “$path” ]
Close Window [ Current Window ]
Go to Record/Request/Page [ Next; Exit after last ]
End Loop
This will export every record in the found set as an individual file into the folder "someFolder" located in the user's Documents folder, using the contents of the YourTable::Somefield field as the filename.
If, as you say, you don't need the separate files, then of course this can be much simpler.
More tinkering solved this. The crucial bit was to change the syntax. The script now reads:
tell application "FileMaker Pro"
activate
tell current record to set MyFileName to cell "WebAbstractFileName"
tell current record to set MyWebAbstract to cell "WebAbstract"
end tell
What seems to happen is that the fields must be visible (yet I had that not be a problem at one point...go figure. If they're visible, you can drop the table specification). This script, wrapped in a Loop block, will act on the found set and (if instructed) exit after the last record.
I append the bbedit script to create a new file and save it with a variable taken from another field in case it's of interest.
tell application "BBEdit"
set notesPath to ":Users:ophiochos:Dropbox:TL Conference Admin:Webpage materials:Abstracts:"
set newFilePath to notesPath & MyFileName & ".html"
set newDoc to make new text document with properties {contents:MyWebAbstract}
tell newDoc
set source language to "HTML"
save to newFilePath
close window
end tell
end tell
Or you could simply create a calculated field that contains the contents of the desired export file for each record, loop through the records one by one, and just use an Export Field Contents ['YourCalculatedField_c'] script step.
You can also use FM to preview the html output by using a web viewer to display your html. This, along with exporting individually, you can get all this functionality without the need for an external program.
If you need to change the text encoding for output files, you can also specify those in an xslt for outputting files:
http://filemakerhacks.com/2012/09/23/export-field-contents-as-utf-8/
Also, if performing applescript from within FileMaker, the "tell application" line is not needed since it is implied.

How can i create a new iCal event from file names in a folder?

Usually i quickly update my TODO list creating a new empty file named like this:
2013-10-01 Tell a friend that stackoverflow rocks
2013-10-23 Prepare my super meeting about coding
and so on..
i just need a workflow or applescript that take all file in the folder, extract the date and the title from the file name and creates a new iCal event on that day with that title!
it seems so easy, but how can i achieve that?
Here's something in straight Applescript.
Note: It depends on you changing your date format to DD-MM-YYYY (due to Applescripts in built date parser)
tell application "Finder"
set data_folder to folder POSIX file "/Users/me/Desktop/my_ical_data"
set all_items to every item of data_folder
end tell
set my text item delimiters to {" "}
repeat with cur_item in all_items
set nm to the name of cur_item
set event_date to date (text item 1 of nm)
set event_desc to (text items 2 thru -1 of nm) as string
tell application "iCal"
tell calendar "Work" -- name of calendar you wish to add to
make new event with properties {summary:event_desc, start date:event_date, description:""}
end tell
end tell
end repeat
This does not require you to change the date format in System Preferences:
tell application "Finder" to name of items of folder POSIX file "/Users/username/todo"
repeat with l in result
set s to text ((offset of space in l) + 1) thru -1 of l
set d to current date
tell d to set {year, month, date, time} to {text 1 thru 4 of s, text 6 thru 7 of s, text 9 thru 10 of s, 0}
tell application "iCal" to tell calendar "Work"
make new event with properties {summary:s, start date:d}
end tell
end repeat

Using AppleScript to Input large amounts of dates into Mac Calendar

I am new to AppleScript and I cannot seem to find any good sources to help me with my problem.
Given a file with dates and descriptions:
September 5, 2013
Event 1.
September 8, 2013
Event 2.
etc.
I want to parse the file for the Date and event information, then create events in the Mac Calendar app that are at these days (with these descriptions ad the event titles). However, I am stuck with the following code:
tell application "Finder"
set Names to paragraphs of (read (choose file with prompt "Pick text file containing track names"))
repeat with nextLine in Names
set x to "Thursday, " & nextLine & " 12:00:00 AM"
if nextLine starts with "Sept" then
tell application "Calendar"
tell calendar "My Calendar"
make new event with properties {description:"Event Description", summary:"Event Name", location:"Event Location", start date:date x, allday event:true}
end tell
end tell
end if
end repeat
end tell
The code does not work yet because it complains about the improper date format, and on top of that I have no idea how I could get the second line to be read with the date line.
Any help would be appreciated, Thanks.
I solved the date parsing issue on my system by changing the external file format to:
5 September 2013
This could be a local system date format setting, as that is how mine is defined in the Language & Text - System Preference. Explore your settings first perhaps.
I've reworked your script a bit below to show you how to get at the event name by changing the loop to an index based one.
Also i've dropped the tell "Finder" block as it is not needed, you were not using any commands from the Finder in this case.
I also renamed some of your variables to be more legible (subjective), other tweaks are commented in the script.
set cal_data to the paragraphs of (read (choose file with prompt "Pick text file containing track names"))
set c to the count of cal_data
repeat with i from 1 to c
set currentLine to item i of cal_data
if currentLine contains "September" then
set dt to date (currentLine) -- Dropped the time as an all day event does not need this and the date parse will auto set it to 12:00am
set ev_name to item (i + 1) of cal_data -- The next item in cal_data should be the event name
tell application "Calendar"
tell calendar "My Calendar"
make new event with properties {description:"Event Description", summary:ev_name, location:"Event Location", start date:dt, allday event:true}
end tell
end tell
end if
end repeat
Hopefully this should get you onto the next steps.

Apple Script to change all the events in iCal's end date to one specific date

I am new to Mac. I have some 50 recurring events where the end date is not specified.
I am planning to change all the recurring events to a specific end date. I am looking for an apple script to change it in one run. Thanks!!
The following script should do what you need. You will need to change the title to the name of the calendar you are interested from "Home". And you will need to change the end date from "20130902" to whatever you need (in YYYYMMDD format).
For whatever reason, iCal seems to subtract 1 day from this end date, so in this example, the end dates will all be set to 1st Sept 2013.
tell application "Calendar"
repeat with thisCal in (calendars whose title is "Home")
setEndDate of me to "20130902" for thisCal
end repeat
end tell
on setEndDate to endDate for thisCalendar
tell application "Calendar"
repeat with thisEvent in (events of thisCalendar)
if recurrence of thisEvent is not missing value and recurrence of thisEvent does not contain "UNTIL" then
set recurrence of thisEvent to recurrence of thisEvent & ";UNTIL=" & endDate
end if
end repeat
end tell
end setEndDate

in Xcode using apple script, check for system date at launch of application to validate

I am writing a mac application using Xcode with applescript base. I want to add in a set of code so upon program launch, it would compare with system date. If date within range specified, proceed. If date check is out of range then terminate program immediately.
Can anyone offer any suggestions? thank you!
Xcode: 3.2.5
OSX 10.6 Snow Leopard
Do you mean something like this?
set range to 3 * days
set targetDate to (date "Saturday, March 19, 2011 12:00:00 AM")
set currDate to current date
if abs(targetDate - currDate) > range then
display dialog "quit"
else
display dialog "continue"
end if
on abs(n)
if n < 0 then
return -n
else
return n
end if
end abs

Resources