Jump to content
Sign in to follow this  
Krishan

If Current Time - TimeIn > 4 hrs

Recommended Posts

Hi.

I've got a little problem and I would be grateful if somebody could help me...

I have a database called "Users.fp5" and it has a field called "TimeIn" which contains the last time that the user signed in on my website.

I have a another field called "Signed" which indicates whether or not the user is signed "In" or "Out".

I would like an applescript to make the field "Signed" to equal "Out" if there is more than a 4 hour difference between the "TimeIn" value and the current time.

Since I'm planning to use this applescript as a method of automatically signing (timing) out users, then I would also like this applescript to work every hour.

Can anyone help me?

Thank a lot in advance!

Share this post


Link to post
Share on other sites

Create a number calculation field that is set to equal the time field that you want to compare the current time to. Make it a number result to make the comparision using AppleScript easier.

The write a script like this:

code:

on idle

Share this post


Link to post
Share on other sites

Thanks Chuck...

I'll try it out. I don't really know how to take into account the midnight problem because I'm a beginner in Applescript. But thanks a lot, it's a great help!

Share this post


Link to post
Share on other sites

quote:

I don't really know how to take into account the midnight problem because I'm a beginner in Applescript.

Well, one way that I can think of off the top of my head is to check to see if the TimeIn field is greater than 20 * hours, and if so, subtract 24 * hours from it. This would make it a negative number that could be compared to the current time with success.

code:

on idle

Share this post


Link to post
Share on other sites

Hello!

Thank you very much for your help. I really appreciate it.

I tried out your applescript and I changed a few things.

I created a number calculation to find out the number of seconds of the "TimeIn" field and called it "TimeInNo".

I ended up using the applescript below:

-----------------------------------------

set myDate to current date

set currentTime to time of myDate

tell application "FileMaker Pro"

tell database "Users.fp5"

tell layout "cgi"

tell current record

set fieldTime to cell "TimeInNo"

if (currentTime <= (4 * hours)) then

if (fieldTime >= (20 * hours)) then

set fieldTime to fieldTime - (24 * hours)

end if

end if

if (currentTime - fieldTime >= 4 * hours) then

set cell "Signed" to "Out"

end if

end tell

end tell

end tell

end tell

-----------------------------------------

I've tested it with different times and it seems to work properly.

Thanks for all your guidance and time!!

Share this post


Link to post
Share on other sites

I'll try and rewrite my code:

<blockquote>

set myDate to current date

set currentTime to time of myDate

tell application "FileMaker Pro"

tell database "Users.fp5"

tell layout "cgi"

tell current record

set fieldTime to cell "TimeInNo"

if (currentTime <= (4 * hours)) then

if (fieldTime >= (20 * hours)) then

set fieldTime to fieldTime - (24 * hours)

end if

end if

if (currentTime - fieldTime >= 4 * hours) then

set cell "Signed" to "Out"

end if

end tell

end tell

end tell

end tell

</blockquote>

Share this post


Link to post
Share on other sites

how do you write your code so that the format stays?

I'll try again....

code:

set myDate to current date

set currentTime to time of myDate

tell application "FileMaker Pro"

tell database "Users.fp5"

tell layout "cgi"

tell current record

set fieldTime to cell "TimeInNo"

if (currentTime <= (4 * hours)) then

if (fieldTime >= (20 * hours)) then

set fieldTime to fieldTime - (24 * hours)

end if

end if

if (currentTime - fieldTime >= 4 * hours) then

set cell "Signed" to "Out"

end if

end tell

end tell

end tell

end tell


Share this post


Link to post
Share on other sites

If I use the code below... and run my applescript, will it continue to run every 30 minutes automatically?

code:

on idle

set myDate to current date

set currentTime to time of myDate

tell application "FileMaker Pro"

tell database "Users.fp5"

tell layout "cgi"

tell current record

set fieldTime to cell "TimeInNo"

if (currentTime <= (4 * hours)) then

if (fieldTime >= (20 * hours)) then

set fieldTime to fieldTime - (24 * hours)

end if

end if

if (currentTime - fieldTime >= 4 * hours) then

set cell "Signed" to "Out"

end if

end tell

end tell

end tell

end tell

return (30 * minutes)

end idle


I'm still working out how to check all the records..... then I'll be able to put it in my code. Is there anyway to say "check all records"?

If anyone can help me.... I'd be really grateful!! Thank you.

Share this post


Link to post
Share on other sites

I saw your question about applying this to all records in the Scriptmaker forum. I assumed that you were operating within a Filemaker script rather than an Applescript, so what I wrote there won't necessarily apply. However, I suggest that you set up a FileMaker script to set your fields and only use Applescript to trigger it. Based on the code Chuck already gave you, I would do this:

code:

on idle

-- get the current date

set myDate to current date

-- extract the time from the current date

set currentTime to time of myDate

tell document "Users.fp5" of application "FileMaker Pro"

perform script Filemaker Script "SignOutUsers"

end tell

-- come back and check again in 1 hour.

return (1 * hours)

end idle


And then your Filemaker script "SignOutUsers" would look like this:

code:

Show all records

Go to Record/Request [first]

Loop

If [signed="In"]

Set Field[signed, Calculation=case(Status(CurrentTime)-TimeIn<0,"Out",Status(CurrentTime)-TimeIn>4,"Out","In")]

End If

Go to Record/Request[next][exit after last]

End loop


My reason for suggesting a combination of FM script and Applescript, is that I find FM scripts easier to debug, because they can be easily run and debugged directly within Filemaker. Also, chances are that in the future, you will likely want to add other record operations on a periodic basis, and you can do this within the same filemaker script without touching the applescript. Of course, this is just my own preference. smile.gif" border="0

[ May 20, 2001: Message edited by: BobWeaver ]

Share this post


Link to post
Share on other sites

Hello.

Thank you very much for helping me. I think using an applescript to call a FM script is a great idea.... and I will try to use the scripts you gave me.

I was wondering if you could please explain to me the meaning of the script line below:

code:

Set Field[signed, Calculation=case(Status(CurrentTime)-TimeIn<0,"Out",Status(CurrentTime)-TimeIn>4,"Out","In")]


When I was writing my applescript, I had to make the TimeIn into seconds. CurrentTime was also in seconds and this meant that at midnight the time would be reset to 0 seconds. So when the Current Time was between 12 and 4 am the equation "Current Time - TimeIn" would be a negative number, since TimeIn would be a greater number of seconds than the CurrentTime. Does ScriptMaker's script have this problem?

Thanks for your time.

Share this post


Link to post
Share on other sites

Hello Bob and Chuck.

I tried out all your scripts.

What I am now using is a script within FileMaker's ScriptMaker and this script is called "TimeIn". It looks like this:

code:

Show all records

Go to Record/Request [first]

Loop

Perform Applescript [set myDate......]

Go to Record/Request[next][exit after last]

End loop


And the Applescript that is within this "TimeIn" script is:

code:

set myDate to current date

set currentTime to time of myDate

tell application "FileMaker Pro"

tell database "Users.fp5"

tell layout "cgi"

tell current record

set fieldTime to cell "TimeInNo"

if (currentTime <= (4 * hours)) then

if (fieldTime >= (20 * hours)) then

set fieldTime to fieldTime - (24 * hours)

end if

end if

if (currentTime - fieldTime >= 4 * hours) then

set cell "Signed" to "Out"

end if

end tell

end tell

end tell

end tell


What do you think about these scripts?

I tried getting the following Applescript to work in order to activate my "TimeIn" script every 30 minutes but it didn't seem to work:

code:

on idle

set myDate to current date

set currentTime to time of myDate

tell application "FileMaker Pro"

tell database "Users.fp5"

activate script "TimeIn"

end tell

end tell

return (30 * minutes)

end idle


Also, applescript doesn't understand the line

code:

perform script Filemaker Script "TimeIn"


so.... I'm a bit stuck. I would like to use an applescript to activate my "TimeIn" script every 30 mins.

I know I'm asking a lot! But I would be really grateful for some more help.

Thank you very much.

Share this post


Link to post
Share on other sites

For some reason FileMaker made the command to trigger a FileMaker Script from AppleScrip "do script".

Chuck

Share this post


Link to post
Share on other sites

Thank Chuck. I used the "Do script...." line and it worked!

So my applescript to trigger the FMPro "TimeIn" script is:

code:

on idle

set myDate to current date

set currentTime to time of myDate

tell application "FileMaker Pro"

tell database "Users.fp5"

do script "TimeIn"

end tell

end tell

return (30 * minutes)

end idle


wow.... thanks a lot Bob and Chuck!!!! That is really amazing.... I couldn't have done any of it without you guys.

Thanks for all your time. smile.gif" border="0

Share this post


Link to post
Share on other sites

um...... I still have a problem. oops.

I'm using:

code:

on idle

set myDate to current date

set currentTime to time of myDate

tell application "FileMaker Pro"

tell database "Users.fp5"

do script "TimeIn"

end tell

end tell

return (30 * minutes)

end idle


But....

The "on idle" and "end idle" commands stop my applescript from working.

So I've tried using

code:

set myDate to current date

set currentTime to time of myDate

tell application "FileMaker Pro"

tell database "Users.fp5"

do script "TimeIn"

end tell

end tell

return (30 * minutes)


And my applescript works but the "return (30 * minutes)" command doesn't work either.

Can you please help me create a repeating script?

There are also a number of ways you can save an applescript. Which one would you suggest I use?

Thank you.

Share this post


Link to post
Share on other sites

Krishan,

you can also use the delay command it takes seconds so you would have to write something like:

repeat

--Your code here

delay 1800

end repeat

Share this post


Link to post
Share on other sites

What do you mean that the idle handler stops your script from working? Do you mean that it doesn't run the FileMaker script when you first double-click on the applet?

You need the idle handler in order to receive the 30 minute return. Make sure that your script applet is set to stay open when you save it.

Chuck

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.