4
$\begingroup$

I'm not able to use URLFetch in my $UserBaseDirectory/Kernel/Init.m as it always returns ImportString[Null, Text, CharacterEncoding -> "ISO8859-1"].

Here is my test code, which works in a Notebook, but runs forever in the init file:

ip = $Failed;

While[ip == $Failed,
    	ip = Quiet@Check[URLFetch@"http://tyilo.com/ip/", $Failed]
];
MessageDialog[ip];

The reason I would like to use URLFetch in my init script is that I have distributed it to a couple of friends and I would like to make it automatically update. Everything except the URLFetch part of the auto-update script works, so it would be nice to be able to finish this feature.

If you could make it run asynchronously after Mathematica has fully started up, that would be fine too.

$\endgroup$
6
  • 2
    $\begingroup$ There are other things which do not work at this stage of the initialization. I remember that pattern matching of string didn't work correctly either. We had a discussion about this here and when I remember correctly, we came to the conclusion that it has something to do with JLink which is not functional at this stage. $\endgroup$
    – halirutan
    Commented Sep 24, 2013 at 0:08
  • $\begingroup$ You could consider putting the update in $Epilog instead. (I haven't tried it and it should be noted that it's invoked when returning from a Dialog as well, which may not be a good thing in this case.) Be careful about using things like MessageDialog, they're not command line friendly. $\endgroup$
    – Szabolcs
    Commented Sep 24, 2013 at 0:38
  • $\begingroup$ @halirutan It seems Needs["JLink`"]; JLink`InstallJava[] does work in init.m, but it doesn't fix URLFetch. $\endgroup$
    – Szabolcs
    Commented Sep 24, 2013 at 0:40
  • $\begingroup$ @Szabolcs I now remember: When I had the first version of the automcompletion, which only worked in version 9.0.0, I had the code in init.m. Back then, the check for the OS like If[StringMatchQ[$SystemID,"Linux-*"],.. did not work. I thought we (I mean literaly, you and me) had spoken about it. $\endgroup$
    – halirutan
    Commented Sep 24, 2013 at 0:57
  • $\begingroup$ The conclusion was that StringMatchQ uses some java stuff for the regex matching. $\endgroup$
    – halirutan
    Commented Sep 24, 2013 at 0:58

2 Answers 2

1
$\begingroup$

By using a scheduled task I made the following workaround:

Module[{t},
    t = CreateScheduledTask[
        "initFileScheduledTask";
        If[URLFetch@"http://example.org/" =!= Null,
            t = Select[ScheduledTasks[],
                StringMatchQ[ToString@#[[2]], StartOfString ~~ "initFileScheduledTask" ~~ ___] &
            ];
            RemoveScheduledTask @ t;
            ip = Quiet@Check[URLFetch@"http://tyilo.com/ip/", $Failed];
            MessageDialog @ ip;
        ],
        1
    ];
    StartScheduledTask @ t;
];
$\endgroup$
1
$\begingroup$

The code from the original post works correctly when placed in init.m as of version 11.2.0.

For more details, see the following answer.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.