Function Repository Resource:

ObjectExistsQ

Source Notebook

Check if an object exists

Contributed by: Bob Sandheinrich

ResourceFunction["ObjectExistsQ"][obj]

gives True if the object exists and False otherwise.

ResourceFunction["ObjectExistsQ"]["Register",head,test]

registers test as the function to call for objects of Head head in ResourceFunction["ObjectExistsQ"].

Details and Options

URLs are considered to exist if an http request gives a 2XX or 3XX status code.
Tasks are considered to exist if the have not completed.
ResourceFunction["ObjectExistsQ"] returns False for unknown object types.
More object types can be added for the kernel session using ResourceFunction["ObjectExistsQ"]["Register",].

Examples

Basic Examples (3) 

Check if a PermissionsGroup exists:

In[1]:=
ResourceFunction["ObjectExistsQ"][PermissionsGroup["bobsbesties"]]
Out[1]=

Use CreatePermissionsGroup to create the group:

In[2]:=
CreatePermissionsGroup["bobsbesties"];

Now it exists:

In[3]:=
ResourceFunction["ObjectExistsQ"][PermissionsGroup["bobsbesties"]]
Out[3]=

Scope (34) 

BoxObject and CellObject (3) 

Create a BoxObject and CellObject:

In[4]:=
cell = ResourceFunction[
   "PrintAsCellObject"][{ResourceFunction["WolfieSay"][
    "Are you my bestie?"], DynamicWrapper[ResourceFunction["BirdSay"]["Umm....."], box = EvaluationBox[]]}]
Out[4]=

Both exist:

In[5]:=
ResourceFunction["ObjectExistsQ"] /@ {box, cell}
Out[5]=

Both can be deleted with NotebookDelete:

In[6]:=
NotebookDelete@box
In[7]:=
ResourceFunction["ObjectExistsQ"] /@ {box, cell}
Out[7]=
In[8]:=
NotebookDelete@cell
In[9]:=
ResourceFunction["ObjectExistsQ"] /@ {box, cell}
Out[9]=

ChannelObject (2) 

Using ChannelObject does not actually create a channel:

In[10]:=
ResourceFunction["ObjectExistsQ"][ChannelObject["BestiesChatChannel"]]
Out[10]=

Using CreateChannel makes the channel live:

In[11]:=
CreateChannel[ChannelObject["BestiesChatChannel"]];
In[12]:=
ResourceFunction["ObjectExistsQ"][ChannelObject["BestiesChatChannel"]]
Out[12]=

CloudObject (2) 

Using CloudObject does not actually create a cloud object:

In[13]:=
ResourceFunction["ObjectExistsQ"][CloudObject["BestiesInfo"]]
Out[13]=

Using CloudPut or similar functions creates the object:

In[14]:=
CloudPut[<|
   "Joel" -> <|"Sweet WL Coder" -> True, "Baseball Fan" -> True, "Professional Wrester" -> False|>|>, "BestiesInfo"];
In[15]:=
ResourceFunction["ObjectExistsQ"][CloudObject["BestiesInfo"]]
Out[15]=

Databin (1) 

Check if a Databin exists:

In[16]:=
ResourceFunction["ObjectExistsQ"][
Databin["I5b8CGbd"]]
Out[16]=

DatabaseReference (1) 

A DatabaseReference exists if it is backed by a file that exists or if it is connected:

In[17]:=
ResourceFunction["ObjectExistsQ"][
 DatabaseReference[FindFile["ExampleData/ecommerce-database.sqlite"]]]
Out[17]=
In[18]:=
ResourceFunction["ObjectExistsQ"][
 DatabaseReference["bobsbesties.sqlite"]]
Out[18]=

ExternalSessionObject (3) 

External sessions exist while they are active:

In[19]:=
session = StartExternalSession["Python"]
Out[19]=
In[20]:=
ResourceFunction["ObjectExistsQ"][session]
Out[20]=

Use DeleteObject to end the session:

In[21]:=
DeleteObject[session]

Now, the session no longer exists:

In[22]:=
ResourceFunction["ObjectExistsQ"][session]
Out[22]=

File (1) 

For a File, ObjectExistsQ is equivalent to FileExistsQ:

In[23]:=
ResourceFunction["ObjectExistsQ"][File[$InstallationDirectory]]
Out[23]=
In[24]:=
ResourceFunction["ObjectExistsQ"][File["/besties/former/shunned"]]
Out[24]=

LocalObject (2) 

For a LocalObject, ObjectExistsQ is equivalent to FileExistsQ:

In[25]:=
ResourceFunction["ObjectExistsQ"][LocalObject["BestiesPhoto"]]
Out[25]=

Use Put to create the local object:

In[26]:=
Put[ResourceFunction["FirstWebImage"]["Friends"], LocalObject["BestiesPhoto"]];
In[27]:=
ResourceFunction["ObjectExistsQ"][LocalObject["BestiesPhoto"]]
Out[27]=

NotebookObject (2) 

A NotebookObject exists only if the window has not been closed:

In[28]:=
nb = CreateDocument[TextCell["An Essay About My Besties", "Title"]]
Out[28]=
In[29]:=
ResourceFunction["ObjectExistsQ"][nb]
Out[29]=

Closing the window destroys the NotebookObject:

In[30]:=
NotebookClose[nb]
In[31]:=
ResourceFunction["ObjectExistsQ"][nb]
Out[31]=

PermissionsGroup (3) 

Check if a PermissionsGroup exists:

In[32]:=
ResourceFunction["ObjectExistsQ"][PermissionsGroup["bobsbesties"]]
Out[32]=

Create the group with CreatePermissionsGroup:

In[33]:=
CreatePermissionsGroup["bobsbesties"];

Now it exists:

In[34]:=
ResourceFunction["ObjectExistsQ"][PermissionsGroup["bobsbesties"]]
Out[34]=

PersistentObject (3) 

Using PersistentObject does not actually create a persistent object:

In[35]:=
po = PersistentObject["MyBestBestie", "KernelSession"]
Out[35]=
In[36]:=
ResourceFunction["ObjectExistsQ"][po]
Out[36]=

Assign a value to create the object:

In[37]:=
po["Value"] = "Joel"
Out[37]=

Now it exists:

In[38]:=
ResourceFunction["ObjectExistsQ"][po]
Out[38]=

ResourceFunction (2) 

Check if a ResourceFunction exists:

In[39]:=
rf = DefineResourceFunction[AppendTo[$BobsBesties, #] &, "AddToBesties"]
Out[39]=
In[40]:=
ResourceFunction["ObjectExistsQ"][rf]
Out[40]=

Delete the resource:

In[41]:=
DeleteObject[rf]
In[42]:=
ResourceFunction["ObjectExistsQ"][rf]
Out[42]=

Functions published in the Wolfram Function Repository always exist:

In[43]:=
ResourceFunction["ObjectExistsQ"][ResourceFunction["BirdSay"]]
Out[43]=
In[44]:=
ResourceFunction["BirdSay"]["I ain't migratin' nowhere"]
Out[44]=

ResourceObject (1) 

Check if a ResourceObject exists:

In[45]:=
ResourceFunction["ObjectExistsQ"][ResourceObject["Bob"]]
Out[45]=

SearchIndexObject (2) 

Check if a SearchIndexObject exists:

In[46]:=
index = CreateSearchIndex["ExampleData/Text"]
Out[46]=
In[47]:=
ResourceFunction["ObjectExistsQ"][index]
Out[47]=

Delete the index:

In[48]:=
DeleteSearchIndex[index]
In[49]:=
ResourceFunction["ObjectExistsQ"][index]
Out[49]=

TaskObject (2) 

A task only exists until it is complete:

In[50]:=
task = SessionSubmit[
  ScheduledTask[Speak["Bob Loves His Besties"], {3}]]
Out[50]=
In[51]:=
ResourceFunction["ObjectExistsQ"][task]
Out[51]=

Once completed, it no longer exists:

In[52]:=
Pause[5]; ResourceFunction["ObjectExistsQ"][task]
Out[52]=

URL (1) 

See if a URL exists:

In[53]:=
ResourceFunction["ObjectExistsQ"][
 URL["https://www.twitter.com/wolframtap"]]
Out[53]=
In[54]:=
ResourceFunction["ObjectExistsQ"][
 URL["https://www.twitter.com/bobsbesties"]]
Out[54]=

Streams (2) 

Test if an InputStream or OutputStream is open:

In[55]:=
Put["BobsBesties"]
In[56]:=
stream = OpenRead["BobsBesties"]
Out[56]=
In[57]:=
ResourceFunction["ObjectExistsQ"][stream]
Out[57]=

Close the stream:

In[58]:=
Close[stream]
Out[58]=
In[59]:=
ResourceFunction["ObjectExistsQ"][stream]
Out[59]=

Strings (1) 

Strings are either interpreted as URLs or files:

In[60]:=
ResourceFunction["ObjectExistsQ"]["http://www.wikipedia.org"]
Out[60]=
In[61]:=
ResourceFunction["ObjectExistsQ"][$InstallationDirectory]
Out[61]=
In[62]:=
ResourceFunction[
 "ObjectExistsQ"]["this string is treated as a file path"]
Out[62]=

Properties and Relations (3) 

Unknown object types give False:

In[63]:=
ResourceFunction["ObjectExistsQ"][BestieObject["Joel"]]
Out[63]=

Register a new object type:

In[64]:=
ResourceFunction["ObjectExistsQ"]["Register", BestieObject, MemberQ[$MyBesties, First[#]] &]
Out[64]=

Now the specified function is used on the object type to determine whether it exists:

In[65]:=
$MyBesties = {"Joel"};
In[66]:=
ResourceFunction["ObjectExistsQ"][BestieObject["Joel"]]
Out[66]=
In[67]:=
ResourceFunction["ObjectExistsQ"][BestieObject["Joseph"]]
Out[67]=

Version History

  • 2.0.0 – 12 December 2019
  • 1.0.0 – 20 November 2019

Related Resources

License Information